Facebook Pixel

Maximal Square

Given a binary matrix, find out the largest size square sub-matrix with all 1's and return its area.

Input & Output
Input
matrix — a binary matrix
Output
the area of the largest square in the input matrix
Example
Input
matrix = 
[[1, 0, 1, 0, 0],
 [1, 0, 1, 1, 1],
 [1, 1, 1, 1, 0],
 [1, 0, 0, 1, 0]]
Output
4
Explanation

The largest square is of size 2x2 and area 4.

Constraints
  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 300
  • matrix[i][j] is 0 or 1

Try it yourself

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro