#pragma region 라이브러리
#include <iostream>
//#include <format>
#include <cmath>
#include <functional>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
using namespace std;
#pragma endregion
#pragma region 최적화용
#define FAST_IO() ios::sync_with_stdio(0); cin.tie(0);
#define y first
#define x second
#define INIT 9876543210
#define MAX_SIZE 8
using IntPair = pair<int, int>;
using DoubleVec = vector<vector<int>>;
using Vec = vector<int>;
// 상하좌우
IntPair dir[]
{
{ 1, 0 },
{-1, 0 },
{ 0, -1 },
{ 0, 1 }
};
#pragma endregion
// 화이트
string wb[]
{
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW"
};
// 블랙
string bb[]
{
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB",
"BWBWBWBW",
"WBWBWBWB"
};
vector<string> board;
int GetCnt(int y, int x, int type = 0)
{
int cnt = 0;
for (int i = 0; i < MAX_SIZE; i++)
{
for (int j = 0; j < MAX_SIZE; j++)
{
if (!type)
cnt += (board[y + i][x + j] != wb[i][j]);
else
cnt += (board[y + i][x + j] != bb[i][j]);
}
}
return cnt;
}
int main()
{
int n, m, ans, res = INIT;
cin >> n >> m;
board.resize(n);
for (int i = 0; i < n; i++)
cin >> board[i];
for (int y = 0; y <= n - MAX_SIZE; y++)
{
for (int x = 0; x <= m - MAX_SIZE; x++)
{
ans = min(GetCnt(y, x), GetCnt(y, x, 1));
res = min(res, ans);
}
}
cout << res;
return 0;
}