#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int sum = 0;
  char c = getchar();
  bool f = 0;
  while (c < '0' || c > '9') {
    if (c == '-') f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  if (f) return -sum;
  return sum;
}
inline char getc() {
  char c = getchar();
  while (c != 'W' && c != 'B') c = getchar();
  return c == 'B';
}
const int N = 55, M = 2505, INF = 1e5;
int n, m, cnt, ID[N][N];
bool c[M], w[M][5];
int to[M][5], len[M], d[M];
const int mx[] = {0, 0, 1, -1};
const int my[] = {1, -1, 0, 0};
inline void link(int x, int y) {
  to[x][++len[x]] = y;
  w[x][len[x]] = c[x] ^ c[y];
}
inline int BFS(int S) {
  int i, j, x, y;
  deque<int> q;
  for (i = 1; i <= cnt; i++) d[i] = INF;
  q.push_front(S);
  d[S] = 1;
  while (!q.empty()) {
    x = q.front();
    q.pop_front();
    for (j = len[x], i = 1; i <= j; i++) {
      y = to[x][i];
      if (d[y] != INF) continue;
      if (w[x][i])
        d[y] = d[x] + 1, q.push_back(y);
      else
        d[y] = d[x], q.push_front(y);
    }
  }
  int ans = 0;
  for (i = 1; i <= cnt; i++)
    if (c[i]) ans = max(ans, d[i]);
  return ans;
}
int main() {
  int i, j, k, x, y;
  n = read();
  m = read();
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) c[ID[i][j] = ++cnt] = getc();
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      for (k = 0; k < 4; k++) {
        x = i + mx[k];
        y = j + my[k];
        if (x >= 1 && x <= n && y >= 1 && y <= m) link(ID[i][j], ID[x][y]);
      }
  int ans = INF;
  for (i = 1; i <= cnt; i++) ans = min(ans, BFS(i));
  printf("%d", ans);
  return 0;
}
