정답 풀이
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
char[][] map = new char[N][M];
String[] stringMap = new String[N];
for(int i=0;i<N;i++){
stringMap[i] = br.readLine();
map[i] = stringMap[i].toCharArray();
}
int minVal = Integer.MAX_VALUE;
for(int i=0;i<=N-8;i++){
for(int j=0;j<=M-8;j++){
int cnt = 0;
for(int start=i;start<i+8;start++){
char[] temp;
if((start-i)%2==0){
temp = "BWBWBWBW".toCharArray();
}
else{
temp = "WBWBWBWB".toCharArray();
}
for(int startj=j;startj<j+8;startj++){
if(map[start][startj]!=temp[startj-j]){
cnt++;
}
}
}
minVal = Math.min(minVal,cnt);
cnt = 0;
for(int start=i;start<i+8;start++){
char[] temp;
if((start-i)%2==0){
temp = "WBWBWBWB".toCharArray();
}
else{
temp = "BWBWBWBW".toCharArray();
}
for(int startj=j;startj<j+8;startj++){
if(map[start][startj]!=temp[startj-j]){
cnt++;
}
}
}
minVal = Math.min(minVal,cnt);
}
}
System.out.println(minVal);
}
}