#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void R(T &xx) {
  xx = 0;
  char ch = getchar();
  bool F = 0;
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') F = 1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    xx = xx + xx + (xx << 3) + ch - 48, ch = getchar();
  if (F) xx = -xx;
}
template <class T>
inline void add(int &x, T y) {
  for (x += y; x >= 51123987; x -= 51123987)
    ;
}
int n, m;
char s[55][55];
int d[55][55];
bool u[55][55];
pair<int, int> q[55 * 55 * 55];
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int sol(int x, int y) {
  q[1] = make_pair(x, y);
  memset(d, 6, sizeof(d));
  d[x][y] = 1;
  memset(u, 0, sizeof(u));
  for (int h = 0, t = 1; h < t;) {
    h++;
    u[q[h].first][q[h].second] = 0;
    for (int i = 0; i < (int)4; i++) {
      int x = q[h].first + dx[i], y = q[h].second + dy[i];
      if (x < 0 || x >= n || y < 0 || y >= m) continue;
      int tmp =
          d[q[h].first][q[h].second] + (s[x][y] != s[q[h].first][q[h].second]);
      if (d[x][y] > tmp) {
        d[x][y] = tmp;
        if (!u[x][y]) q[++t] = make_pair(x, y), u[x][y] = 1;
      }
    }
  }
  int res = 0;
  for (int i = 0; i < (int)n; i++)
    for (int j = 0; j < (int)m; j++)
      if (s[i][j] == 'B') res = max(res, d[i][j]);
  return res;
}
int main() {
  R(n);
  R(m);
  for (int i = 0; i < (int)n; i++) scanf("%s", s[i]);
  int ans = (1000000111);
  for (int i = 0; i < (int)n; i++)
    for (int j = 0; j < (int)m; j++) ans = min(ans, sol(i, j));
  printf("%d\n", ans);
  return 0;
}
