#include <bits/stdc++.h>
using namespace std;
const int oo = 1047483647, maxn = 11000,
          fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int n, i, j, k, m, h[55][55], tot, d[2555], q[2555];
bool sol[2555], a[55][55];
char ch;
struct nod {
  int next[20100], y[20100], g[2510], tt;
  void add(int i, int j) {
    next[++tt] = g[i];
    g[i] = tt;
    y[tt] = j;
  }
} e;
void dfs(int x, int y) {
  h[x][y] = tot;
  int x1, y1, i;
  for (i = 0; i <= 3; i++) {
    x1 = x + fx[i][0];
    y1 = y + fx[i][1];
    if (x1 < 1 || y1 < 1 || x1 > n || y1 > m || a[x1][y1] != a[x][y] ||
        h[x1][y1])
      continue;
    dfs(x1, y1);
  }
}
int get(int x) {
  memset(d, -1, sizeof d);
  int l = 1, r = 0;
  d[x] = 0;
  q[++r] = x;
  while (l <= r) {
    x = q[l];
    for (int p = e.g[x]; p; p = e.next[p]) {
      int j = e.y[p];
      if (d[j] == -1) {
        q[++r] = j;
        d[j] = d[x] + 1;
      }
    }
    ++l;
  }
  int tm = 0, i;
  for (i = 1; i <= tot; i++)
    if (sol[i]) tm = max(tm, d[i]);
  return tm;
}
int main() {
  scanf("%d%d\n", &n, &m);
  bool black = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      scanf("%c", &ch);
      a[i][j] = (ch == 'B');
      if (a[i][j]) black = 1;
    }
    scanf("\n");
  }
  if (!black) {
    printf("0");
    return 0;
  }
  e.tt = 0;
  memset(e.g, 0, sizeof e.g);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      if (!h[i][j]) {
        ++tot, dfs(i, j);
        if (a[i][j]) sol[tot] = 1;
      }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      for (k = 0; k <= 3; k++) {
        int x1, y1;
        x1 = i + fx[k][0];
        y1 = j + fx[k][1];
        if (x1 < 1 || y1 < 1 || x1 > n || y1 > m || h[x1][y1] == h[i][j])
          continue;
        e.add(h[x1][y1], h[i][j]);
        e.add(h[i][j], h[x1][y1]);
      }
  int ans = oo;
  for (i = 1; i <= tot; i++) {
    int tmp = get(i);
    ans = min(ans, tmp);
  }
  printf("%d", ans + 1);
  return 0;
}
