mediumDynamic Programming 2DPure DSA~25 min
Largest All-Healthy Square Region
A datacenter floor is a grid where each cell is healthy (1) or faulted (0). Find the area of the largest square block consisting entirely of healthy cells.
Problem
Given an m x n binary matrix, return the area (side²) of the largest square whose cells are all 1s. If there is no 1, return 0.
Input
A 2D matrix of characters or integers '0'/'1'.
Output
An integer — the area of the largest all-ones square.
Constraints
- 1 ≤ m, n ≤ 300
- Each cell is 0 or 1.
Examples
Example 1
Input
[[1,0,1,0,0],[1,0,1,1,1],[1,1,1,1,1],[1,0,0,1,0]]
Output
4
A 2x2 square of 1s exists; no 3x3 fits, so area = 4.
Example 2
Input
[[0,1],[1,0]]
Output
1
Largest all-ones square is a single cell.