Since getting his job at Iceberg Corporation, Rami has been ignoring his tasks and playing a new game called Grid Crash. It is a game played on a grid $$$G$$$ of size $$$n\times m.$$$ Each cell in the grid is either black, white, or is empty.
Initially, each cell $$$(i,j)$$$ in the grid contains some color $$$c_{i,j}.$$$
Now the game is played as follows:
Now, Rami wants to finish the game in the fewest moves possible. Please help him.
A single integer $$$r:$$$ the minimum number of moves to win the game.
5 5BWBBBWWBWWBWBWBBWBBBWWWWW
3
5 5WWWWWWBBBWWWWWWWBBBWWWWWW
2
5 5WBBBWBWBWBBBWBBBWBWBWBBBW
3
The following example shows a sequence of moves that solves the first game (test case 1) in $$$3$$$ moves. It can be shown that it is optimal: