#include <bits/stdc++.h>
using namespace std;
const int MAXN = 50;
const int INF = 0x3f3f3f3f;
template <class T>
void Read(T &x) {
  x = 0;
  char c = getchar();
  bool flag = 0;
  while (c < '0' || '9' < c) {
    if (c == '-') flag = 1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  if (flag) x = -x;
}
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
bool minimize(int &x, int v) {
  if (x > v) {
    x = v;
    return true;
  } else
    return false;
}
char str[MAXN + 5];
bool G[MAXN + 5][MAXN + 5];
int dist[MAXN + 5][MAXN + 5];
int m, n;
queue<pair<int, int> > que;
int bfs(int sx, int sy) {
  while (!que.empty()) que.pop();
  memset(dist, 0x3f, sizeof(dist));
  dist[sx][sy] = 0;
  que.push(make_pair(sx, sy));
  pair<int, int> now, newed;
  while (!que.empty()) {
    now = que.front(), que.pop();
    for (int i = 0; i < 4; ++i) {
      newed.first = now.first + dx[i];
      newed.second = now.second + dy[i];
      if (newed.first < 1 || newed.first > m || newed.second < 1 ||
          newed.second > n)
        continue;
      if (minimize(
              dist[newed.first][newed.second],
              dist[now.first][now.second] +
                  (G[now.first][now.second] ^ G[newed.first][newed.second])))
        que.push(newed);
    }
  }
  int ret = 0;
  for (int i = 1; i <= m; ++i)
    for (int j = 1; j <= n; ++j)
      if (G[i][j]) ret = max(ret, dist[i][j]);
  return ret;
}
int main() {
  bool flag = false;
  Read(m), Read(n);
  for (int i = 1; i <= m; ++i) {
    scanf("%s", str + 1);
    for (int j = 1; j <= n; ++j) flag |= (G[i][j] = (str[j] == 'B'));
  }
  if (!flag)
    puts("0");
  else {
    int ans = INF;
    for (int i = 1; i <= m; ++i)
      for (int j = 1; j <= n; ++j) ans = min(ans, bfs(i, j) + 1);
    printf("%d\n", ans);
  }
}
