#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > q;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool p[55][55];
int d[55][55];
char s[55][55];
int main() {
  int i, j, k, n, m, ans, x, y, ds, co, t, l;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) scanf("%s", s[i]);
  for (i = 0, ans = 1 << 15; i < n; i++)
    for (j = 0; j < m; j++) {
      memset(d, 1, sizeof d);
      memset(p, 0, sizeof p);
      for (d[i][j] = 0, q.push(make_pair(i, j)); !q.empty();) {
        x = q.front().first, y = q.front().second;
        q.pop();
        p[x][y] = false, co = s[x][y], ds = d[x][y];
        for (k = 0; k < 4; k++) {
          x += dx[k], y += dy[k];
          if (0 <= x && x < n && 0 <= y && y < m &&
              ds + (s[x][y] != co) < d[x][y]) {
            d[x][y] = ds + (s[x][y] != co);
            if (!p[x][y]) {
              p[x][y] = true;
              q.push(make_pair(x, y));
            }
          }
          x -= dx[k], y -= dy[k];
        }
      }
      for (t = -1, k = 0; k < n; k++)
        for (l = 0; l < m; l++)
          if (s[k][l] == 'B' && d[k][l] > t) t = d[k][l];
      if (t < ans) ans = t;
    }
  printf("%d\n", ans + 1);
  return 0;
}
