import java.io.*;
import java.util.*;
public class Main {
static boolean[][] chessboard;
static int N, M, countMin;
static boolean[][] white = { { true, false, true, false, true, false, true, false },
{ false, true, false, true, false, true, false, true, },
{ true, false, true, false, true, false, true, false },
{ false, true, false, true, false, true, false, true, },
{ true, false, true, false, true, false, true, false },
{ false, true, false, true, false, true, false, true, },
{ true, false, true, false, true, false, true, false },
{ false, true, false, true, false, true, false, true, } };
static boolean[][] black = { { false, true, false, true, false, true, false, true, },
{ true, false, true, false, true, false, true, false },
{ false, true, false, true, false, true, false, true, },
{ true, false, true, false, true, false, true, false },
{ false, true, false, true, false, true, false, true, },
{ true, false, true, false, true, false, true, false },
{ false, true, false, true, false, true, false, true, },
{ true, false, true, false, true, false, true, false } };
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
chessboard = new boolean[N][M];
for (int i = 0; i < N; i++) {
String str = br.readLine();
for (int j = 0; j < M; j++) {
if (str.charAt(j) == 'W')
chessboard[i][j] = true;
else
chessboard[i][j] = false;
}
}
for (int i = 0; i <= N - 8; i++) {
for (int j = 0; j <= M - 8; j++) {
chess(i, j, white, chessboard);
chess(i, j, black, chessboard);
}
}
System.out.println(countMin);
}
static void chess(int n, int m, boolean[][] pan, boolean[][] chessboard) {
int count = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (chessboard[n + i][m + j] != pan[i][j])
count++;
}
}
countMin = Math.min(count, countMin);
}
}