#include <bits/stdc++.h>
using namespace std;
int v[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
char a[55][55];
int pre[10005];
int last[2505];
int w[10005];
int e[10005];
int d[2505];
int r, c, m;
int bfs(int x) {
  deque<int> q;
  int i, j;
  q.push_back(x);
  memset(d, 0x3f, sizeof(d));
  d[x] = 0;
  while (!q.empty()) {
    x = q.front();
    q.pop_front();
    for (i = last[x]; i != 0; i = pre[i])
      if (d[e[i]] > d[x] + w[i]) {
        d[e[i]] = d[x] + w[i];
        if (w[i])
          q.push_back(e[i]);
        else
          q.push_front(e[i]);
      }
  }
  d[0] = 0;
  for (i = 1; i <= r; ++i)
    for (j = 1; j <= c; ++j)
      if (a[i][j] == 'B') d[0] = max(d[0], d[(i - 1) * c + j]);
  return d[0];
}
int main() {
  int i, j, k, x, y, ans = -1;
  scanf("%d%d", &r, &c);
  for (i = 1; i <= r; ++i) scanf("%s", a[i] + 1);
  for (i = 1; i <= r; ++i)
    for (j = 1; j <= c; ++j) {
      if (a[i][j] == 'B') ans = 0x7fffffff;
      for (k = 0; k < 4; ++k) {
        x = i + v[k][0], y = j + v[k][1];
        if ((x > 0 && x <= r && y > 0 && y <= c))
          (e[++m] = (x - 1) * c + y, w[m] = a[i][j] != a[x][y],
           pre[m] = last[(i - 1) * c + j], last[(i - 1) * c + j] = m);
      }
    }
  for (i = 1; i <= r * c; ++i) ans = min(ans, bfs(i));
  printf("%d\n", ans + 1);
  return 0;
}
