#include <bits/stdc++.h>
using namespace std;
char str[55][55] = {0};
long n, m, dist[55][55] = {0};
long dx[4] = {0, 0, 1, -1};
long dy[4] = {1, -1, 0, 0};
long work(long x, long y) {
  long i, j;
  long ans = -1;
  deque<pair<long, long> > Q;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) dist[i][j] = 2000000000;
  dist[x][y] = 0;
  Q.push_back(pair<long, long>(x, y));
  for (; !Q.empty();) {
    long x = Q.front().first, y = Q.front().second;
    Q.pop_front();
    for (i = 0; i < 4; i++) {
      long xx, yy;
      xx = x + dx[i];
      yy = y + dy[i];
      if (str[xx][yy] != 'W' && str[xx][yy] != 'B') continue;
      if (dist[x][y] + (str[x][y] != str[xx][yy]) < dist[xx][yy]) {
        dist[xx][yy] = dist[x][y] + (str[x][y] != str[xx][yy]);
        if (str[x][y] != str[xx][yy])
          Q.push_back(pair<long, long>(xx, yy));
        else
          Q.push_front(pair<long, long>(xx, yy));
      }
    }
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if (str[i][j] == 'B') ans = max(dist[i][j], ans);
  return ans + 1;
}
int main() {
  long i, j;
  long ans = 2000000000;
  scanf("%ld%ld", &n, &m);
  for (i = 1; i <= n; i++) scanf("%s", str[i] + 1);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) ans = min(work(i, j), ans);
  printf("%ld\n", ans);
  return 0;
}
