#t274. 方块涂色
方块涂色
Coloring the Flag
Problem Background
Special for beginners, ^_^
Problem Description
A country's law states that a flag made of small squares is legal as long as it satisfies the following rules (a certain bear: achoo—):
- The top several rows (at least one row) are entirely white;
- The next several rows (at least one row) are entirely blue;
- The remaining rows (at least one row) are entirely red.
Now there is a chessboard-like cloth divided into rows and columns of cells, each cell being white, blue, or red. Xiao a wants to turn this cloth into a legal flag of that country, by painting some cells to cover their previous colors.
Xiao a is lazy and wants to paint the fewest cells so that the cloth becomes a legal flag.
Input Format
The first line contains two integers .
The next lines form a matrix where each cell is one of W (white), B (blue), R (red).
Output Format
One integer: the minimum number of cells that must be painted.
Sample Input
4 5
WRWRW
BWRWB
WRWRW
RWBWR
Sample Output
11
Sample Explanation
The target state is:
WWWWW
BBBBB
RRRRR
RRRRR
which requires changing cells in total.
Constraints
For of the data, .