Problem Statement
Given a m x n binary matrix mat (containing only 0s and 1s), return the total number of square submatrices that have all ones.
Example 1:
Input:
mat = [ [0,1,1,1], [1,1,1,1], [0,1,1,1] ]
Output: 15
Explanation:
- 10 squares of size 1×1
- 4 squares of size 2×2
- 1 square of size 3×3
Total = 15
Example 2:
Input:
mat = [ [1,0,1], [1,1,0], [1,1,0] ]
Output: 7
Constraints:
- 1 <= m, n <= 300
- mat[i][j] is 0 or 1
Approach in Plain English
- We want all squares with all ones in the matrix.
- Use dynamic programming (DP):
- Let
dp[i][j]= size of the largest square ending at(i, j). - If
mat[i][j] == 0,dp[i][j] = 0. - If
mat[i][j] == 1and not in first row/column, then:dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])Explanation: A square can be extended only if top, left, and top-left cells also form squares.
- Let
- Sum all
dp[i][j]values → total squares.
Key idea: DP stores size of largest square ending at each cell, which implicitly counts smaller squares too.
Beginner-Friendly Dry Run
Take matrix:
mat = [ [0,1,1], [1,1,1], [0,1,1] ]
- Initialize
dpmatrix same size asmat:
dp = [ [0,0,0], [0,0,0], [0,0,0] ]
- Fill
dp:
- Cell (0,0) → mat[0][0] = 0 → dp[0][0] = 0
- Cell (0,1) → mat[0][1] = 1 → dp[0][1] = 1
- Cell (0,2) → mat[0][2] = 1 → dp[0][2] = 1
dp = [ [0,1,1], [0,0,0], [0,0,0] ]
- Cell (1,0) → mat[1][0] = 1 → dp[1][0] = 1
- Cell (1,1) → mat[1][1] = 1 → min(dp[0][1], dp[1][0], dp[0][0]) = min(1,1,0) = 0 → dp[1][1] = 1
- Cell (1,2) → mat[1][2] = 1 → min(dp[0][2], dp[1][1], dp[0][1]) = min(1,1,1) = 1 → dp[1][2] = 2
dp = [ [0,1,1], [1,1,2], [0,0,0] ]
- Cell (2,0) → mat[2][0] = 0 → dp[2][0] = 0
- Cell (2,1) → mat[2][1] = 1 → min(dp[1][1], dp[2][0], dp[1][0]) = min(1,0,1) = 0 → dp[2][1] = 1
- Cell (2,2) → mat[2][2] = 1 → min(dp[1][2], dp[2][1], dp[1][1]) = min(2,1,1) = 1 → dp[2][2] = 2
dp = [ [0,1,1], [1,1,2], [0,1,2] ]
- Sum all dp values → total squares = 0+1+1 + 1+1+2 + 0+1+2 = 9
Textual Approach
- Use DP matrix same size as input.
- For each cell
(i,j):- If
mat[i][j] == 0→ dp[i][j] = 0 - Else if
i==0orj==0→ dp[i][j] = 1 - Else → dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])
- If
- Sum all dp values → answer.
Java Code
public class CountSquareSubmatrices {
public static int countSquares(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int[][] dp = new int[m][n];
int total = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 1) {
if (i == 0 || j == 0) {
dp[i][j] = 1;
} else {
dp[i][j] = 1 + Math.min(dp[i-1][j-1],
Math.min(dp[i-1][j], dp[i][j-1]));
}
total += dp[i][j];
}
}
}
return total;
}
public static void main(String[] args) {
int[][] mat1 = {
{0,1,1},
{1,1,1},
{0,1,1}
};
System.out.println(countSquares(mat1)); // Output: 9
int[][] mat2 = {
{1,0,1},
{1,1,0},
{1,1,0}
};
System.out.println(countSquares(mat2)); // Output: 7
}
}
Key Points
- DP stores largest square ending at each cell, which implicitly counts smaller squares.
- Time Complexity: O(m*n) → traverse entire matrix once.
- Space Complexity: O(m*n) → DP matrix (can optimize to O(n) if needed).
- Beginner tip: The
minfunction ensures the square can only grow if all three neighbors support it.
