Darken the Board
grid is a panel of lights given as a list of rows: 1 is a light that is on and 0 one that is off. Pressing a cell switches that light and each of its side neighbours (up, down, left and right) that exist: every one of them goes from on to off or from off to on. Return the smallest number of presses that turns every light off, or -1 if that cannot be done. The order of the presses does not matter, and pressing the same cell twice is the same as not pressing it at all. Do not change grid.
Example 1
Pressing the top-left cell switches it and its two neighbours, which are exactly the three lights that are on.
Example 2
Pressing the three cells other than the bottom-right one switches the top-left light three times and every other light twice, so only the top-left light changes.
Example 3
Either press switches both lights, so the two lights always stay different and can never both be off.
Constraints
1 <= rows, cols <= 6- every cell is 0 or 1
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.