Description

Given an m x n binary matrix filled with 0s and 1s, find the largest square containing only 1s and return its area.

Examples

Input:matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output:4
Explanation:

2×2 square of 1's.

Input:matrix = [["1","1","1"],["1","1","1"],["1","1","1"]]
Output:9
Explanation:

The entire 3×3 matrix consists of 1's, forming a perfect square. The area of this 3×3 square is 9.

Input:matrix = [["1","1","0","1"],["1","1","0","1"],["0","0","1","1"],["1","1","1","1"]]
Output:4
Explanation:

There are multiple 2×2 squares of 1's possible: top-left corner (positions [0,0] to [1,1]), right side (positions [0,3] to [1,3] - but blocked by 0's), and bottom-right corner (positions [2,2] to [3,3]). The largest square has area 4.

Constraints

  • 1 ≤ m, n ≤ 300

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!