#include <bits/stdc++.h>
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
const int Maxn = 55;
const int Maxm = Maxn * Maxn;
int First[Maxm], dis[Maxm], Next[Maxm * 4], to[Maxm * 4], cnt, we[Maxm * 4],
    q[Maxn][Maxn], n, m, ans = 1 << 30;
bool vis[Maxm];
inline void ch(int x) {
  if (x < ans) ans = x;
  return;
}
inline void add(int z, int y, int w) {
  Next[++cnt] = First[z];
  First[z] = cnt;
  to[cnt] = y;
  we[cnt] = w;
}
inline int R() {
  char c;
  int res;
  while ((c = getchar()) > '9' || c < '0')
    ;
  res = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0';
  return res;
}
std::queue<int> Q;
inline int SPFA(int x) {
  memset(dis, 63, sizeof dis);
  Q.push(x);
  dis[x] = 0;
  int pos, res = 0;
  while (!Q.empty()) {
    pos = Q.front();
    Q.pop();
    vis[pos] = 0;
    for (int k = First[pos]; k; k = Next[k])
      if (dis[pos] + we[k] < dis[to[k]]) {
        dis[to[k]] = dis[pos] + we[k];
        if (!vis[to[k]]) Q.push(to[k]);
        vis[to[k]] = 1;
      }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (q[i][j] == 1)
        res = (res > dis[(i + (j - 1) * n)] ? res : dis[(i + (j - 1) * n)]);
  return res;
}
int main() {
  n = R();
  m = R();
  char c;
  bool f = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      while ((c = getchar()) != 'B' && c != 'W')
        ;
      q[i][j] = (c == 'B');
      f |= (c == 'B');
    }
  if (!f) {
    puts("0");
    return 0;
  }
  for (int x = 1; x <= n; x++)
    for (int y = 1; y <= m; y++)
      for (int i = 0; i < 4; i++)
        if (((x + dx[i]) > 0 && (x + dx[i]) <= n) &&
            ((y + dy[i]) > 0 && (y + dy[i]) <= m)) {
          if (q[x][y] == q[(x + dx[i])][(y + dy[i])])
            add((x + (y - 1) * n), ((x + dx[i]) + ((y + dy[i]) - 1) * n), 0);
          else
            add((x + (y - 1) * n), ((x + dx[i]) + ((y + dy[i]) - 1) * n), 1);
        }
  for (int x = 1; x <= n; x++)
    for (int y = 1; y <= m; y++) ch(SPFA((x + (y - 1) * n)));
  printf("%d\n", ans + 1);
}
