# Number of square matrices with all 1s

Given an N*M matrix containing only 0s and 1s, the task is to count the number of square submatrices containing all 1s.

Examples:

Input: arr[][] = {{0, 1, 1, 1},
{1, 1, 1, 1},
{0, 1, 1, 1}}
Output: 15
Explanation:
There are 10 squares of side length 1.
There are 4 squares of side length 2.
There is 1 square of side length 3.
Total number of squares = 10 + 4 + 1 = 15.

Input: arr[][] = {{1, 0, 1},
{1, 1, 0},
{1, 1, 0}}
Output: 7

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

Approach: This problem can be solved using Dynamic Programming.

1. Let the array arr[i][j] store the number of square matrices ending at (i, j)
2. The recurrence relation to find the number of squares ending at (i, j) can be given by:
• If arr[i][j] is 1:
• arr[i][j] = min( min(arr[i-1][j], arr[i][j-1]), arr[i-1][j-1]) + 1
• Else if arr[i][j] is 0:
• arr[i][j] = 0
3. Calculate the sum of the array which is equal to the number of square submatrices with all 1s.

Below is the implementation of the above approach:

## CPP

 `// C++ program to return the number of ` `// square submatrices with all 1s ` `#include ` `using` `namespace` `std; ` ` `  `#define n 3 ` `#define m 3 ` ` `  `// Function to return the number of ` `// square submatrices with all 1s ` `int` `countSquareMatrices(``int` `a[][m], ` `                        ``int` `N, ``int` `M) ` `{ ` `    ``// Initialize count variable ` `    ``int` `count = 0; ` ` `  `    ``for` `(``int` `i = 1; i < N; i++) { ` `        ``for` `(``int` `j = 1; j < M; j++) { ` `            ``// If a[i][j] is equal to 0 ` `            ``if` `(a[i][j] == 0) ` `                ``continue``; ` ` `  `            ``// Calculate number of ` `            ``// square submatrices ` `            ``// ending at (i, j) ` `            ``a[i][j] = min(min(a[i - 1][j], ` `                              ``a[i][j - 1]), ` `                          ``a[i - 1][j - 1]) ` `                      ``+ 1; ` `        ``} ` `    ``} ` ` `  `    ``// Calculate the sum of the array ` `    ``for` `(``int` `i = 0; i < N; i++) ` `        ``for` `(``int` `j = 0; j < M; j++) ` `            ``count += a[i][j]; ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[][m] = { { 1, 0, 1 }, ` `                     ``{ 1, 1, 0 }, ` `                     ``{ 1, 1, 0 } }; ` ` `  `    ``cout << countSquareMatrices(arr, n, m); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to return the number of  ` `// square submatrices with all 1s ` `class` `GFG  ` `{ ` `     `  `    ``final` `static` `int` `n = ``3``;  ` `    ``final` `static` `int` `m = ``3``;  ` `     `  `    ``// Function to return the number of  ` `    ``// square submatrices with all 1s  ` `    ``static` `int` `countSquareMatrices(``int` `a[][], ``int` `N, ``int` `M)  ` `    ``{  ` `        ``// Initialize count variable  ` `        ``int` `count = ``0``;  ` `     `  `        ``for` `(``int` `i = ``1``; i < N; i++) ` `        ``{  ` `            ``for` `(``int` `j = ``1``; j < M; j++)  ` `            ``{  ` `                ``// If a[i][j] is equal to 0  ` `                ``if` `(a[i][j] == ``0``)  ` `                    ``continue``;  ` `     `  `                ``// Calculate number of  ` `                ``// square submatrices  ` `                ``// ending at (i, j)  ` `                ``a[i][j] = Math.min(Math.min(a[i - ``1``][j], a[i][j - ``1``]),  ` `                            ``a[i - ``1``][j - ``1``]) + ``1``;  ` `            ``}  ` `        ``}  ` `     `  `        ``// Calculate the sum of the array  ` `        ``for` `(``int` `i = ``0``; i < N; i++)  ` `            ``for` `(``int` `j = ``0``; j < M; j++)  ` `                ``count += a[i][j];  ` `     `  `        ``return` `count;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args) ` `    ``{  ` `        ``int` `arr[][] = { { ``1``, ``0``, ``1` `},  ` `                        ``{ ``1``, ``1``, ``0` `},  ` `                        ``{ ``1``, ``1``, ``0` `} };  ` `     `  `        ``System.out.println(countSquareMatrices(arr, n, m));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python

 `# Python3 program to return the number of ` `# square submatrices with all 1s ` `n ``=` `3` `m ``=` `3` ` `  `# Function to return the number of ` `# square submatrices with all 1s ` `def` `countSquareMatrices(a, N, M): ` `     `  `    ``# Initialize count variable ` `    ``count ``=` `0` ` `  `    ``for` `i ``in` `range``(``1``, N): ` `        ``for` `j ``in` `range``(``1``, M): ` `             `  `            ``# If a[i][j] is equal to 0 ` `            ``if` `(a[i][j] ``=``=` `0``): ` `                ``continue` ` `  `            ``# Calculate number of ` `            ``# square submatrices ` `            ``# ending at (i, j) ` `            ``a[i][j] ``=` `min``([a[i ``-` `1``][j],  ` `                      ``a[i][j ``-` `1``], a[i ``-` `1``][j ``-` `1``]])``+``1` ` `  `    ``# Calculate the sum of the array ` `    ``for` `i ``in` `range``(N): ` `        ``for` `j ``in` `range``(M): ` `            ``count ``+``=` `a[i][j] ` ` `  `    ``return` `count ` ` `  `# Driver code ` ` `  `arr ``=` `[ [ ``1``, ``0``, ``1``], ` `    ``[ ``1``, ``1``, ``0` `], ` `    ``[ ``1``, ``1``, ``0` `] ] ` ` `  `print``(countSquareMatrices(arr, n, m)) ` ` `  `# This code is contributed by mohit kumar 29 `

## C#

 `// C# program to return the number of  ` `// square submatrices with all 1s ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``static` `int` `n = 3;  ` `    ``static` `int` `m = 3;  ` `     `  `    ``// Function to return the number of  ` `    ``// square submatrices with all 1s  ` `    ``static` `int` `countSquareMatrices(``int` `[,]a, ``int` `N, ``int` `M)  ` `    ``{  ` `        ``// Initialize count variable  ` `        ``int` `count = 0;  ` `     `  `        ``for` `(``int` `i = 1; i < N; i++) ` `        ``{  ` `            ``for` `(``int` `j = 1; j < M; j++)  ` `            ``{  ` `                ``// If a[i][j] is equal to 0  ` `                ``if` `(a[i, j] == 0)  ` `                    ``continue``;  ` `     `  `                ``// Calculate number of  ` `                ``// square submatrices  ` `                ``// ending at (i, j)  ` `                ``a[i, j] = Math.Min(Math.Min(a[i - 1, j], a[i, j - 1]),  ` `                            ``a[i - 1, j - 1]) + 1;  ` `            ``}  ` `        ``}  ` `     `  `        ``// Calculate the sum of the array  ` `        ``for` `(``int` `i = 0; i < N; i++)  ` `            ``for` `(``int` `j = 0; j < M; j++)  ` `                ``count += a[i, j];  ` `     `  `        ``return` `count;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main() ` `    ``{  ` `        ``int` `[,]arr = { { 1, 0, 1 },  ` `                        ``{ 1, 1, 0 },  ` `                        ``{ 1, 1, 0 } };  ` `     `  `        ``Console.WriteLine(countSquareMatrices(arr, n, m));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

Output :

`7`

Time Complexity: O(N*M)

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : mohit kumar 29, AnkitRai01