N, M = map(int, input().split())
map_list = [list(input()) for _ in range(N)]

min_num = None

for i in range(N - 7):
    for j in range(M - 7):
        num1, num2 = 0, 0
        # W
        for k in range(i, i + 8):
            for l in range(j, j + 8):
                if((k + l - i - j) % 2 == 0):
                    if(map_list[k][l] == 'B'):
                        num1 += 1
                else:
                    if(map_list[k][l] == 'W'):
                        num1 += 1
                        
        #B         
        for k in range(i, i + 8):
            for l in range(j, j + 8):
                if((k + l - i - j) % 2 == 0):
                    if(map_list[k][l] == 'W'):
                        num2 += 1
                else:
                    if(map_list[k][l] == 'B'):
                        num2 += 1
                        
        change_box = num1 if num1 < num2 else num2
        if(min_num is None):
            min_num = change_box
        else:
            min_num = change_box if min_num > change_box else min_num
            
print(min_num)

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 1107번(python)  (0) 2019.07.10
백준 1436번(python)  (1) 2019.07.08
백준 7568번(python)  (0) 2019.07.07
백준 2231번(python)  (0) 2019.07.07
백준 2798번(python)  (0) 2019.07.06