#K33492. Minimum Operations to Uniform Grid

    ID: 25099 Type: Default 1000ms 256MiB

Minimum Operations to Uniform Grid

Minimum Operations to Uniform Grid

You are given a grid with (N) rows and (M) columns filled with binary digits (0s and 1s). Your task is to determine the minimum number of operations required to make the grid uniform, where all cells have the same value (either all 0s or all 1s). An operation consists of choosing any rectangular subgrid and flipping every bit within that subgrid (i.e. changing 0 to 1 and 1 to 0).

Note:
If the grid is already uniform, no operations are needed.
The flipping operation can be mathematically understood as applying a bitwise NOT to a submatrix. In LaTeX: if (A) is the matrix, and (B) is a submatrix of (A), then the operation is
[ B_{ij} \leftarrow 1 - B_{ij} \quad \text{for all valid } i, j ]

The grid is considered uniform if (\forall i,j,\ A_{ij} = c) where (c) is either 0 or 1.

inputFormat

The input is given via standard input (stdin) and has the following format:

  • The first line contains two space-separated integers (N) and (M), representing the number of rows and columns, respectively.
  • The following (N) lines each contain (M) integers (either 0 or 1) separated by spaces, representing the grid.

outputFormat

Print a single integer to standard output (stdout), which is the minimum number of operations required to make the grid uniform.## sample

3 3
0 0 0
0 0 0
0 0 0
0

</p>