#P1681. 最大正方形II

最大正方形II

Background

After finishing his schoolwork, v 神 could finally do his "real business": taking a walk with his girlfriend. One day, as they walked, they unknowingly arrived at a desolate place. Just as v 神 was about to turn back, he noticed a sign. The sign had a line of small text and a picture. The small text said: "Contact me after you find the largest alternating square in the picture, and this land will be yours." In an era of soaring housing prices, v 神 certainly did not want to miss this opportunity, so he started searching... Of course, with v 神's ability, he couldn't find it. Can you help v 神 find it?

Problem 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