# Find the number of corner rectangles that can be formed from given Matrix

• Last Updated : 30 Sep, 2021

Given a binary matrix mat[][] of dimensions N*M, the task is to find the number of corner rectangles that can be formed. A corner rectangle is defined as the submatrix having 1s on the corners of it and each 1s must belong to a unique cell in that submatrix.

Examples:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: mat[][] = {{1, 0, 1}, {0, 0, 0}, {1, 0, 1}}
Output: 1
Explanation:
There exists only one submatrix satisfying the given formula represented by bold as:
1 0 1
0 0 0
1 0 1

Input: mat[][] = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}
Output: 9

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The given problem can be solved by using the concepts of all distinct possible pairs from N points which are given by NC2. The idea is to store the frequency of pair of cells (i, j) having the values as 1s in the map of pairs, say M. After generating the frequency map find the total count of corners rectangle formed using the above formula. Follow the steps below to solve the given problem:

• Initialize a variable, say count that stores the resultant count of corner rectangle.
• Initialize a map, say m[] that stores the frequency of the cell (i, j) having values as 1.
• Iterate over the range [0, M) using the variable i and perform the following tasks:
• Traverse over the map m[] using the variable it and add the value of it.second*(it.second – 1)/2 to the variable count.
• After performing the above steps, print the value of count as the answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach`` ` `#include ``using` `namespace` `std;`` ` `// Function to find all possible rectangles``// having distinct corners as 1s``int` `countCornerRectangles(``    ``vector >& mat)``{``    ``// Stores the count of rectangles``    ``int` `count = 0;`` ` `    ``int` `N = mat.size();``    ``int` `M = mat.size();`` ` `    ``// Map to store the frequency``    ``map, ``int``> m;`` ` `    ``// Iterate over each row``    ``for` `(``int` `i = 0; i < N; i++) {`` ` `        ``// Iterate over each cell``        ``for` `(``int` `j = 0; j < M; j++) {``            ``if` `(mat[i][j] == 1) {`` ` `                ``// Check for 1's of th``                ``// same column pair``                ``for` `(``int` `k = j + 1;``                     ``k < M; k++) {``                    ``if` `(mat[i][k] == 1) {``                        ``m[{ j, k }]++;``                    ``}``                ``}``            ``}``        ``}``    ``}`` ` `    ``// For any pair of column cells (x, y)``    ``// If the frequency is n. Then choosing``    ``// any 2 will form a rectangle``    ``for` `(``auto``& it : m) {``        ``count``            ``+= (it.second * (it.second - 1)) / 2;``    ``}`` ` `    ``return` `count;``}`` ` `// Driver Code``int` `main()``{``    ``vector > mat``        ``= { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };`` ` `    ``cout << countCornerRectangles(mat);`` ` `    ``return` `0;``}`
Output:
```9
```

Time Complexity: O(N*M2)
Auxiliary Space: O(M2)

My Personal Notes arrow_drop_up