We are given a N*M grid, print the number of rectangles in it.

Examples:

Input : N = 2, M = 2 Output : 9 There are 4 rectangles of size 1 x 1. There are 2 rectangles of size 1 x 2 There are 2 rectangles of size 2 x 1 There is one rectangle of size 2 x 2. Input : N = 5, M = 4 Output : 150 Input : N = 4, M = 3 Output: 60

We have discussed counting number of squares in a n x m grid,

Let us derive a formula for number of rectangles.

If the grid is 1×1, there is 1 rectangle.

If the grid is 2×1, there will be 2 + 1 = 3 rectangles

If it grid is 3×1, there will be 3 + 2 + 1 = 6 rectangles.

we can say that for N*1 there will be N + (N-1) + (n-2) … + 1 = (N)(N+1)/2 rectangles

If we add one more column to N×1, firstly we will have as many rectangles in the 2nd column as the first,

and then we have that same number of 2×M rectangles.

So N×2 = 3 (N)(N+1)/2

After deducing this we can say

For N*M we’ll have (M)(M+1)/2 (N)(N+1)/2 =** M(M+1)(N)(N+1)/4**

So the formula for total rectangles will be M(M+1)(N)(N+1)/4

.

**Combinatorial Logic:**

N*M grid can be represented as (N+1) vertical lines and (M+1) horizontal lines.

In a rectangle, we need two distinct horizontal and two distinct verticals.

So going by the logic of Combinatorial Mathematics we can choose 2 vertical lines and 2 horizontal lines to form a rectangle. And total number of these combinations is the number of rectangle possible in the grid.

Total Number of Rectangles in N*M grid: ** ^{N+1}C_{2} * ^{M+1}C_{2 }**=

**(N*(N+1)/2!)*(M*(M+1)/2!)**=

**N*(N+1)*M*(M+1)/4**

## C++

`// C++ program to count number of rectangles` `// in a n x m grid` `#include <bits/stdc++.h>` `using` `namespace` `std;` `int` `rectCount(` `int` `n, ` `int` `m)` `{` ` ` `return` `(m * n * (n + 1) * (m + 1)) / 4;` `}` `/* driver code */` `int` `main()` `{` ` ` `int` `n = 5, m = 4;` ` ` `cout << rectCount(n, m);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// JAVA Code to count number of ` `// rectangles in N*M grid` `import` `java.util.*;` `class` `GFG {` ` ` ` ` `public` `static` `long` `rectCount(` `int` `n, ` `int` `m)` ` ` `{` ` ` `return` `(m * n * (n + ` `1` `) * (m + ` `1` `)) / ` `4` `;` ` ` `}` ` ` ` ` `/* Driver program to test above function */` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{` ` ` `int` `n = ` `5` `, m = ` `4` `;` ` ` `System.out.println(rectCount(n, m));` ` ` `}` `}` `// This code is contributed by Arnav Kr. Mandal.` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count number ` `# of rectangles in a n x m grid` `def` `rectCount(n, m):` ` ` `return` `(m ` `*` `n ` `*` `(n ` `+` `1` `) ` `*` `(m ` `+` `1` `)) ` `/` `/` `4` `# Driver code ` `n, m ` `=` `5` `, ` `4` `print` `(rectCount(n, m))` `# This code is contributed by Anant Agarwal.` |

*chevron_right*

*filter_none*

## C#

`// C# Code to count number of ` `// rectangles in N*M grid` `using` `System;` `class` `GFG {` ` ` ` ` `public` `static` `long` `rectCount(` `int` `n, ` `int` `m)` ` ` `{` ` ` `return` `(m * n * (n + 1) * (m + 1)) / 4;` ` ` `}` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `Main() ` ` ` `{` ` ` `int` `n = 5, m = 4;` ` ` `Console.WriteLine(rectCount(n, m));` ` ` `}` `}` ` ` `// This code is contributed by Anant Agarwal.` |

*chevron_right*

*filter_none*

## PHP

`<?php` `// PHP program to count ` `// number of rectangles` `// in a n x m grid` `function` `rectCount(` `$n` `, ` `$m` `)` `{` ` ` `return` `(` `$m` `* ` `$n` `* ` ` ` `(` `$n` `+ 1) * ` ` ` `(` `$m` `+ 1)) / 4;` `}` `// Driver Code` `$n` `= 5;` `$m` `= 4;` `echo` `rectCount(` `$n` `, ` `$m` `);` `// This code is contributed` `// by ajit` `?>` |

*chevron_right*

*filter_none*

**Output:**

150

This article is contributed by **Pranav**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of unique rectangles formed using N unit squares
- Number of rectangles in a circle of radius R
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Count the number of rectangles such that ratio of sides lies in the range [a,b]
- Find the minimum number of rectangles left after inserting one into another
- Total number of unit cells covered by all given Rectangles
- Find if two rectangles overlap
- Create a matrix with alternating rectangles of O and X
- Find all rectangles filled with 0
- Total area of two overlapping rectangles
- Sum of Areas of Rectangles possible for an array
- Intersecting rectangle when bottom-left and top-right corners of two rectangles are given
- Smallest square formed with given rectangles
- Check if it is possible to rearrange rectangles in a non-ascending order of breadths
- Maximum given sized rectangles that can be cut out of a sheet of paper
- Largest subset of rectangles such that no rectangle fit in any other rectangle
- Count of distinct rectangles inscribed in an equilateral triangle
- Count Distinct Rectangles in N*N Chessboard
- Check if N rectangles of equal area can be formed from (4 * N) integers
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively