#P1681. Largest Alternating Checkerboard Square
Largest Alternating Checkerboard Square
Largest Alternating Checkerboard Square
You are given a matrix consisting of \(N \times M\) cells, where each cell is colored either black or white. A square submatrix is said to be alternating (or have a checkerboard pattern) if any two adjacent cells (sharing a side) have different colors. In other words, for every two cells that share a common side, their colors are different.
Your task is to find the area of the largest square submatrix that is alternating. The area of a square with side length \(k\) is \(k^2\). Note that a single cell is trivially alternating.
Input and Output: Read the matrix dimensions and the matrix, and output the area of the largest alternating square submatrix.
inputFormat
The first line contains two integers \(N\) and \(M\), the number of rows and columns respectively. The next \(N\) lines each contain a string of length \(M\) consisting only of the characters 'B' (for black) and 'W' (for white), representing the grid.
outputFormat
Output a single integer, which is the area of the largest alternating (checkerboard) square submatrix found in the grid.
sample
3 3
BWB
WBW
BWB
9