Skip to content
Onsite

Maximal Square

MediumP2Coding

matrix is a grid of '0' and '1' characters given as an array of equal-length strings. Return the area of the largest square made only of '1' cells (the number of cells, so a 3 x 3 square has area 9). If there is no '1', return 0.

Example 1

Input
matrix = ["10100","10111","11111","10010"]
Output
4
Why
The 2 x 2 block of ones in the lower right has area 4.

Example 2

Input
matrix = ["01","10"]
Output
1

Example 3

Input
matrix = ["0"]
Output
0

Constraints

  • 1 <= matrix.length, matrix[0].length <= 300
  • Each row has the same length and contains only '0' and '1'.

Hints

Run checks the 3 visible tests, Submit all 11.