| CodEMI 2025 |
|---|
| Finished |
In a busy data center, lamps are essential for smooth server operations. One day, several lamps go out, making it hard for technicians to work. To fix this, the facility uses a special control panel that can toggle all lamps in a selected row or column — flipping each lamp from on to off or off to on.
Your task is to help restore full lighting using the fewest toggles. If it's impossible to turn all lamps on, output $$$-1$$$.
You're given an $$$n \times m$$$ grid representing lamp states: $$$1$$$ for on, $$$0$$$ for off. With each operation, you can toggle any row or column to flip all its lamp states.
Determine the minimum number of toggles needed to turn every lamp on, or report $$$-1$$$ if it's not possible.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2500$$$) — the number of rows and columns in the grid.
The next $$$n$$$ lines each contain $$$m$$$ integers (either $$$0$$$ or $$$1$$$) describing the grid.
For each test case, print a single integer $$$-$$$ the minimum number of toggles required to make all lamps on, or $$$-1$$$ if it is impossible.
3 30 1 01 0 10 1 0
3
4 40 0 1 10 0 1 11 1 0 01 1 0 0
4
4 40 0 0 10 0 0 10 0 0 00 0 0 0
-1
$$$$$$ \begin{matrix} 0 & 1 & 0 \\ 1 & 0 & 1 \\ 0 & 1 & 0 \end{matrix} \xrightarrow{\text{Row 2}} \begin{matrix} 0 & 1 & 0 \\ 0 & 1 & 0 \\ 0 & 1 & 0 \end{matrix} \xrightarrow{\text{Col 1}} \begin{matrix} 1 & 1 & 0 \\ 1 & 1 & 0 \\ 1 & 1 & 0 \end{matrix} \xrightarrow{\text{Col 3}} \begin{matrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{matrix} $$$$$$
| Name |
|---|


