#P1681. 最大正方形II

最大正方形II

Description

There is a grid on the picture, consisting of N×MN\times M cells. Each cell is colored either black or white. Find a square of maximum area whose interior is black-and-white alternating, that is, any two adjacent unit cells (sharing an edge) must not have the same color.

Input Format

The first line contains two integers NN and MM, denoting the number of rows and columns, respectively. Then follow NN lines, each containing MM numbers. Each number is 00 or 11, indicating that the cell is black or white, respectively.

Output Format

Output a single line containing the side length of the largest square that satisfies the condition.

3 3
0 1 0
1 0 0
1 1 1

2

Hint

Sample Explanation: The square from (1,1)(1,1) to (2,2)(2,2) satisfies the condition, and its side length is 22.

Constraints:

  • For 30%30\% of the testdata, N20N \le 20.
  • For 60%60\% of the testdata, N300N \le 300.
  • For 100%100\% of the testdata, N1500N \le 1500.

Translated by ChatGPT 5