#include <iostream>
#include <cstdio>
#include <limits>
using namespace std;
#define INF 987654321
static char B[8][9] = {
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
};
static char W[8][9] = {
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
};
int main() {
std::ios::sync_with_stdio(false);
int N, M;
int cnt, minval = INF;
string arr[50];
cin >> N >> M;
for(int i = 0; i < N; i++)
cin >> arr[i];
for(int i = 0; i < N - 7; i++) {
for(int j = 0; j < M - 7; j++) {
cnt = 0;
for(int x = 0; x < 8; x++) {
for(int y = 0; y < 8; y++) {
if(arr[i + x][j + y] != B[x][y])
cnt++;
}
}
minval = min(minval, cnt);
cnt = 0;
for(int x = 0; x < 8; x++) {
for(int y = 0; y < 8; y++) {
if(arr[i + x][j + y] != W[x][y])
cnt++;
}
}
minval = min(minval, cnt);
}
}
printf("%d", minval);
}