#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int N = 60;
const int M = N * N * 4;
namespace FastIO {
inline int read() {
  int x = 0, F = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') F = ch == '-' ? -1 : 1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + (ch & 15), ch = getchar();
  return x * F;
}
inline void write(int x) {
  if (x < 0) putchar('-'), x = ~x + 1;
  if (x > 9) write(x / 10);
  putchar((x % 10) | 48);
}
inline void write(int x, char ch) { write(x), putchar(ch); }
}  // namespace FastIO
using namespace FastIO;
char s[N][N];
int n, m, ans = INF, dis[N * N], vis[N * N];
struct Graph {
  int tot, to[M], head[M], nxt[M], val[M];
  inline void connect(int u, int v, int w) {
    to[++tot] = v, val[tot] = w;
    nxt[tot] = head[u], head[u] = tot;
  }
} G;
void Spfa(int st) {
  memset(dis, 0x3f, sizeof dis);
  memset(vis, 0, sizeof vis);
  queue<int> q;
  dis[st] = 1, q.push(st);
  while (q.size()) {
    int u = q.front();
    for (int i = G.head[u]; i; i = G.nxt[i]) {
      int v = G.to[i];
      if (dis[v] > dis[u] + G.val[i]) {
        dis[v] = dis[u] + G.val[i];
        vis[v] = 1, q.push(v);
      }
    }
    vis[u] = 0, q.pop();
  }
  int res = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i][j] == 'B') res = max(res, dis[((i - 1) * m + j)]);
  ans = min(ans, res);
}
signed main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (i > 1)
        G.connect(((i - 1) * m + j), ((i - 1 - 1) * m + j),
                  s[i][j] != s[i - 1][j]);
      if (j > 1)
        G.connect(((i - 1) * m + j), ((i - 1) * m + j - 1),
                  s[i][j] != s[i][j - 1]);
      if (i < n)
        G.connect(((i - 1) * m + j), ((i + 1 - 1) * m + j),
                  s[i][j] != s[i + 1][j]);
      if (j < m)
        G.connect(((i - 1) * m + j), ((i - 1) * m + j + 1),
                  s[i][j] != s[i][j + 1]);
    }
  for (int i = 1; i <= n * m; i++) Spfa(i);
  cout << ans << endl;
  return 0;
}
