#K94177. Largest Homogeneous Fruit Square

    ID: 38584 Type: Default 1000ms 256MiB

Largest Homogeneous Fruit Square

Largest Homogeneous Fruit Square

You are given a grid of size \(n \times m\) representing cells that contain either a type of fruit (denoted by an uppercase letter) or an empty cell (denoted by a dot, \('.'\)). Your task is to find the side length of the largest square subgrid that is completely filled with the same type of fruit (i.e. it contains exactly one fruit type) and contains no empty cells.

More formally, the grid is described by \(n\) rows and \(m\) columns. A square subgrid of side length \(s\) is valid if all its \(s^2\) cells contain the same fruit (and not a dot). If no such square exists, output \(0\).

Input Constraints: The values of \(n\) and \(m\) are positive integers. The grid cells consist only of uppercase letters or dots.

Hint: You can use dynamic programming to compute the largest square subgrid ending at each cell.

inputFormat

The first line contains two integers \(n\) and \(m\), the dimensions of the grid.

The next \(n\) lines each contain a string of length \(m\) representing a row of the grid. Each character is either an uppercase letter (representing a fruit) or a dot (\('.'\)) indicating an empty cell.

Input example:

5 5
AAB..
AAB..
CCC..
CC...
CCC..

outputFormat

Output the side length of the largest square subgrid that contains exactly one type of fruit and no empty cells.

For the above example, the output would be:

2
## sample
5 5
AAB..
AAB..
CCC..
CC...
CCC..
2