#K62437. Minimum Operations to Equalize Grid

    ID: 31531 Type: Default 1000ms 256MiB

Minimum Operations to Equalize Grid

Minimum Operations to Equalize Grid

You are given an n x m grid consisting of binary digits (0s and 1s). In one operation, you can choose a subgrid (a contiguous block of cells) and flip all the bits within that subgrid (i.e. change 0 to 1 and 1 to 0). Your task is to determine the minimum number of operations required to make all the cells in the grid equal.

Observation: It turns out that if the grid is not already uniform (i.e. all 0s or all 1s), a single appropriately chosen flip operation can always equalize the grid. Therefore, if the grid contains both 0s and 1s, one operation is enough; otherwise, no operation is needed.

The mathematical formulation can be expressed in \( \LaTeX \) as:

\( \text{answer} = \begin{cases} 0, & \text{if } \text{all cells are equal} \\ 1, & \text{otherwise} \end{cases} \)

This problem tests your ability to work with grids and to make logical deductions based on the properties of binary matrices.

inputFormat

The first line of input contains two space-separated integers \( n \) and \( m \), representing the number of rows and columns of the grid, respectively.

The next \( n \) lines each contain a string of length \( m \) composed of binary characters ('0' or '1'), representing a row of the grid.

For example:

3 3
111
101
111

outputFormat

Output a single integer representing the minimum number of operations required to make all the cells in the grid equal. Output is printed to standard output.

## sample
2 2
01
10
1