#include<iostream>
#include<algorithm>
using namespace std;
const char b[8][9] = {
{"BWBWBWBW"},
{"WBWBWBWB"},
{"BWBWBWBW"},
{"WBWBWBWB"},
{"BWBWBWBW"},
{"WBWBWBWB"},
{"BWBWBWBW"},
{"WBWBWBWB"}
};
const char w[8][9] = {
{"WBWBWBWB"},
{"BWBWBWBW"},
{"WBWBWBWB"},
{"BWBWBWBW"},
{"WBWBWBWB"},
{"BWBWBWBW"},
{"WBWBWBWB"},
{"BWBWBWBW"}
};
int black(int x, int y);
int white(int x, int y);
char board[50][50];
int main()
{
int row, columns, result = 123456;
cin >> row >> columns;
for (int i = 0; i < row; i++)
cin >> board[i];
for (int i = 0; i < row - 7; i++)
{
for (int j = 0; j < columns - 7; j++)
{
result = min(result, min(white(i, j), black(i, j)));
}
}
cout << result << endl;
return 0;
}
int black(int x, int y)
{
int cnt = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (board[x + i][y + j] != b[i][j])
cnt++;
}
}
return cnt;
}
int white(int x, int y)
{
int cnt = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (board[x + i][y + j] != w[i][j])
cnt++;
}
}
return cnt;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPGFsZ29yaXRobT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGNoYXIgYls4XVs5XSA9IHsKICAgIHsiQldCV0JXQlcifSwKICAgIHsiV0JXQldCV0IifSwKICAgIHsiQldCV0JXQlcifSwKICAgIHsiV0JXQldCV0IifSwKICAgIHsiQldCV0JXQlcifSwKICAgIHsiV0JXQldCV0IifSwKICAgIHsiQldCV0JXQlcifSwKICAgIHsiV0JXQldCV0IifQp9Owpjb25zdCBjaGFyIHdbOF1bOV0gPSB7CiAgICB7IldCV0JXQldCIn0sCiAgICB7IkJXQldCV0JXIn0sCiAgICB7IldCV0JXQldCIn0sCiAgICB7IkJXQldCV0JXIn0sCiAgICB7IldCV0JXQldCIn0sCiAgICB7IkJXQldCV0JXIn0sCiAgICB7IldCV0JXQldCIn0sCiAgICB7IkJXQldCV0JXIn0KfTsKCmludCBibGFjayhpbnQgeCwgaW50IHkpOwppbnQgd2hpdGUoaW50IHgsIGludCB5KTsKY2hhciBib2FyZFs1MF1bNTBdOwoKaW50IG1haW4oKQp7CiAgICBpbnQgcm93LCBjb2x1bW5zLCByZXN1bHQgPSAxMjM0NTY7CiAgICBjaW4gPj4gcm93ID4+IGNvbHVtbnM7CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCByb3c7IGkrKykKICAgICAgICBjaW4gPj4gYm9hcmRbaV07CgogICAgZm9yIChpbnQgaSA9IDA7IGkgPCByb3cgLSA3OyBpKyspCiAgICB7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBjb2x1bW5zIC0gNzsgaisrKQogICAgICAgIHsKICAgICAgICAgICAgcmVzdWx0ID0gbWluKHJlc3VsdCwgbWluKHdoaXRlKGksIGopLCBibGFjayhpLCBqKSkpOwogICAgICAgIH0KICAgIH0KICAgIGNvdXQgPDwgcmVzdWx0IDw8IGVuZGw7CiAgICByZXR1cm4gMDsKfQoKaW50IGJsYWNrKGludCB4LCBpbnQgeSkKewogICAgaW50IGNudCA9IDA7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IDg7IGkrKykKICAgIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IDg7IGorKykKICAgICAgICB7CiAgICAgICAgICAgIGlmIChib2FyZFt4ICsgaV1beSArIGpdICE9IGJbaV1bal0pCiAgICAgICAgICAgICAgICBjbnQrKzsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gY250Owp9CgppbnQgd2hpdGUoaW50IHgsIGludCB5KQp7CiAgICBpbnQgY250ID0gMDsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgODsgaSsrKQogICAgewogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgODsgaisrKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKGJvYXJkW3ggKyBpXVt5ICsgal0gIT0gd1tpXVtqXSkKICAgICAgICAgICAgICAgIGNudCsrOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiBjbnQ7Cn0=