P27234 [AtCoder] Russian Flag
提高-
题目描述

Problem Description

Chairman K decided to make a flag to match the IOI 2016 held in Russia. Chairman K first took out an old flag from the warehouse. This flag is divided into an $ N $ by $ M $ grid, and each cell is painted either white, blue, or red.

Chairman K is trying to repaint some of the cells to make the flag look like the Russian flag. In this problem, the Russian flag is defined as follows:

  • The top several rows (at least one row) are all painted white.
  • The following several rows (at least one row) are all painted blue.
  • The remaining rows (at least one row) are all painted red.

Find the minimum number of cells that Chairman K needs to repaint to make the old flag look like the Russian flag.


Input Format

The input consists of $ 1 + N $ lines.

The first line contains two integers $ N, M $ ($3 \leq N \leq 50$, $3 \leq M \leq 50$), separated by a space. This indicates that the flag is divided into an $ N $ by $ M $ grid.

The following $ N $ lines each contain a string of $ M $ characters, representing the color information of the old flag's grid. The $ i $-th line's $ j $-th character ($1 \leq i \leq N$, $1 \leq j \leq M$) is either W, B, or R, representing the color of the cell at the $ i $-th row and $ j $-th column of the old flag. W stands for white, B for blue, and R for red.

Output Format

Output the minimum number of cells that Chairman K needs to repaint to make the old flag look like the Russian flag in one line.


Sample Input and Output

Sample Input #1

4 5
WRWRW
BWRWB
WRWRW
RWBWR

Sample Output #1

11

Sample Input #2

6 14
WWWWWWWWWWWWWW
WBBBWWRRWWBBBW
WWBWWRRRRWWBWW
BWBWWRRRRWWBWW
WBBWWWRRWWBBBW
WWWWWWWWWWWWWW

Sample Output #2

44

Notes/Hints

Sample Explanation 1

In sample input #1, the old flag is colored as shown in the figure below. By repainting the 11 cells marked with X in the figure below, it is possible to make the flag look like the Russian flag. Since it is not possible to make the flag look like the Russian flag by repainting fewer than 11 cells, output 11.

Sample Explanation 2

In sample input #2, the old flag is colored as shown in the figure below.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
4 5 WRWRW BWRWB WRWRW RWBWR
样例输出
11