#include <bits/stdc++.h>
using namespace std;
const int N = 50 + 5;
const int oo = 1000000000;
bool u[N][N];
int n, m;
int ans = -1;
int a[N][N], l[N][N];
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
char s[N];
struct pis {
  int x, y;
  pis() {}
  pis(int x, int y) : x(x), y(y) {}
} f[10 * N * N];
inline int Solve(int x, int y) {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) l[i][j] = oo;
  int front = 0, top = 1;
  f[1] = pis(x, y);
  l[x][y] = 0;
  while (front < top) {
    pis p = f[++front];
    u[p.x][p.y] = 0;
    for (int i = 0; i < 4; i++) {
      int dx = p.x + d[i][0];
      int dy = p.y + d[i][1];
      if (dx >= 1 && dx <= n && dy >= 1 && dy <= m) {
        int dis = a[p.x][p.y] != a[dx][dy];
        if (l[p.x][p.y] + dis < l[dx][dy]) {
          l[dx][dy] = l[p.x][p.y] + dis;
          if (!u[dx][dy]) {
            u[dx][dy] = 1;
            f[++top] = pis(dx, dy);
          }
        }
      }
    }
  }
  int MAX = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] && l[i][j] > MAX) MAX = l[i][j];
  return MAX;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    for (int j = 1; j <= m; j++) {
      if (s[j - 1] == 'B') ans = oo;
      a[i][j] = s[j - 1] == 'W' ? 0 : 1;
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) ans = min(ans, Solve(i, j));
  printf("%d\n", ans + 1);
  return 0;
}
