Maximum Area of Two Non-Overlapping Square Submatrices Problem
#4016
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 4016.
You are given a 2D integer matrix mat of size m × n, where:
mat[r][c] == 1means the cell at rowrand columncis usable.mat[r][c] == 0means it is not usable.
Your task is to find two submatrices that satisfy the following conditions:
- Both submatrices must be squares of the same side length
k. - The two submatrices must not share any cell.
- Each submatrix can only cover cells where
mat[r][c] == 1.
Return the maximum possible area of each of the two squares. If it is not possible to choose two such squares, return 0.
Example 1:

Input: mat = [[1,1,1,0],[1,1,1,1],[0,0,1,1]]
Output: 4
Explanation:
The largest equal non-overlapping squares have side length k = 2 with area 4.
- First square starts at top-left (0, 0) and covers cells (0, 0), (0, 1), (1, 0), and (1, 1).
- Second square starts at top-left (1, 2) and covers cells (1, 2), (1, 3), (2, 2), and (2, 3).
Thus, the answer is 4.
Example 2:

Input: mat = [[0,1],[1,0]]
Output: 1
Explanation:
The largest equal non-overlapping squares have side length k = 1 with area 1.
- First square starts at top-left (0, 1) and covers cell (0, 1).
- Second square starts at top-left (1, 0) and covers cell (1, 0).
Thus, the answer is 1.
Example 3:

Input: mat = [[0,0],[0,1]]
Output: 0
Explanation:
There is only one usable cell, so it is impossible to choose two non-overlapping squares. Thus, the answer is 0.
Constraints:
mat.length == mmat[i].length == n1 <= m, n <= 500mat[i][j]is either 0 or 1.
Solution
Approach: Dynamic Programming + Enumerating Dividing Lines
Two non-overlapping squares can always be separated by a horizontal or a vertical line. Handle the horizontal case (one square above the line, one below) and run the same code on the transposed matrix for the vertical case.
For the horizontal case:
- Going bottom-up, let
f[i][j]be the largest all-1square whose top-left corner is(i, j): ifmat[i][j] = 1, thenf[i][j] = min(f[i+1][j], f[i][j+1], f[i+1][j+1]) + 1. A running maximum over the rows givessuf[i], the largest square inside rows[i, m). - Going top-down, the same recurrence with the square's bottom-right corner gives
pre[i], the largest square inside rows[0, i). - For every line between rows
i - 1andi, both squares need the same side, and a smaller square fits wherever a larger one does, so the side ist = min(pre[i], suf[i])and the area ist * t.
The answer is the best of the horizontal and vertical cases.
Complexity: time O(mn), space O(mn), where m and n are the matrix dimensions.
class Solution:
def maxArea(self, mat: List[List[int]]) -> int:
def calc(mat: List[List[int]]) -> int:
m, n = len(mat), len(mat[0])
f = [[0] * (n + 1) for _ in range(m + 1)]
g = [0] * (m + 1)
suf = [0] * (m + 1)
for i in range(m - 1, 0, -1):
for j in range(n - 1, -1, -1):
if mat[i][j]:
f[i][j] = min(f[i + 1][j], f[i][j + 1], f[i + 1][j + 1]) + 1
g[i] = max(g[i], f[i][j])
suf[i] = max(suf[i + 1], g[i])
f = [[0] * (n + 1) for _ in range(m + 1)]
g = [0] * (m + 1)
pre = [0] * (m + 1)
for i in range(1, m + 1):
for j in range(1, n + 1):
if mat[i - 1][j - 1]:
f[i][j] = min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1
g[i] = max(g[i], f[i][j])
pre[i] = max(pre[i - 1], g[i])
ans = 0
for i in range(1, m):
t = min(pre[i], suf[i])
ans = max(ans, t * t)
return ans
def transpose(mat: List[List[int]]) -> List[List[int]]:
m, n = len(mat), len(mat[0])
ans = [[0] * m for _ in range(n)]
for i in range(m):
for j in range(n):
ans[j][i] = mat[i][j]
return ans
return max(calc(mat), calc(transpose(mat)))