#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int M,N;
char board[51][51];
char reshape[8][8];
vector<int> results;
char w_first[8][8] = {
'W','B','W','B','W','B','W','B',
'B','W','B','W','B','W','B','W',
'W','B','W','B','W','B','W','B',
'B','W','B','W','B','W','B','W',
'W','B','W','B','W','B','W','B',
'B','W','B','W','B','W','B','W',
'W','B','W','B','W','B','W','B',
'B','W','B','W','B','W','B','W'};
char b_first[8][8] = {
'B','W','B','W','B','W','B','W',
'W','B','W','B','W','B','W','B',
'B','W','B','W','B','W','B','W',
'W','B','W','B','W','B','W','B',
'B','W','B','W','B','W','B','W',
'W','B','W','B','W','B','W','B',
'B','W','B','W','B','W','B','W',
'W','B','W','B','W','B','W','B' };
int w_board_reshape(int start_x, int start_y) {
int cnt = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[start_x + i][start_y + j] != w_first[i][j]) {
cnt = cnt + 1;
}
}
}
return cnt;
}
int b_board_reshape(int start_x, int start_y) {
int cnt = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[start_x + i][start_y + j] != b_first[i][j]) {
cnt = cnt + 1;
}
}
}
return cnt;
}
int main() {
cin >> M >> N;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
cin >> board[i][j];
}
}
for (int i = 0; i <= M - 8; i++) {
for (int j = 0; j <= N - 8; j++) {
if (b_board_reshape(i, j) < w_board_reshape(i, j)) {
results.push_back(b_board_reshape(i,j));
}
else {
results.push_back(w_board_reshape(i, j));
}
}
}
int min=*min_element(results.begin(), results.end());
cout << min;
}