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

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:**

Input:mat[][] = {{1, 0, 1}, {0, 0, 0}, {1, 0, 1}}Output:1Explanation:

There exists only one submatrix satisfying the given formula represented by bold as:101

0 0 0101

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

**Approach:** The given problem can be solved by using the concepts of all distinct possible pairs from N points which are given by ** ^{N}C_{2}**. 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:- Iterate over the range
**[0, N)**using the variable**j**and if**mat[i][j]**equals**1**then Iterate over the range**[j_+1, N)**using the variable**k**and if the value of**mat[i][k]**equals**1**then increase the count of**m[{j, k}]**by**1.**

- Iterate over the range
- 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 <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find all possible rectangles ` `// having distinct corners as 1s ` `int` `countCornerRectangles( ` ` ` `vector<vector<` `int` `> >& mat) ` `{ ` ` ` `// Stores the count of rectangles ` ` ` `int` `count = 0; ` ` ` ` ` `int` `N = mat.size(); ` ` ` `int` `M = mat[0].size(); ` ` ` ` ` `// Map to store the frequency ` ` ` `map<pair<` `int` `, ` `int` `>, ` `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<vector<` `int` `> > mat ` ` ` `= { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } }; ` ` ` ` ` `cout << countCornerRectangles(mat); ` ` ` ` ` `return` `0; ` `} ` |

**Output:**

9

**Time Complexity:** O(N*M^{2})**Auxiliary Space:** O(M^{2})