#include <bits/stdc++.h>
using namespace std;
int fx[] = {0, 0, 1, -1};
int fy[] = {1, -1, 0, 0};
struct edge {
  int v, w, nxt;
} e[20001 << 2];
int n, m, head[20001], tot, dis[20001], col[60][60], pot[60][60], bk[20001],
    ans = 1e9 + 1;
char ch[20001];
bool vis[20001], fb;
void add(int u, int v, int w) {
  e[++tot].v = v;
  e[tot].w = w;
  e[tot].nxt = head[u];
  head[u] = tot;
}
void bfs(int s) {
  memset(dis, 0x3f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  queue<int> q;
  q.push(s);
  dis[s] = 0;
  vis[s] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int i = head[u]; i; i = e[i].nxt)
      if (dis[e[i].v] > dis[u] + e[i].w) {
        dis[e[i].v] = dis[u] + e[i].w;
        if (!vis[e[i].v]) {
          vis[e[i].v] = 1;
          q.push(e[i].v);
        }
      }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", ch);
    for (int j = 1; j <= m; ++j) {
      col[i][j] = (ch[j - 1] == 'B');
      fb |= col[i][j];
      pot[i][j] = ++pot[0][0];
      if (col[i][j]) bk[++bk[0]] = pot[i][j];
    }
  }
  if (!fb) {
    puts("0");
    return 0;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      for (int nx, ny, k = 0; k < 4; ++k) {
        nx = i + fx[k];
        ny = j + fy[k];
        if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
        add(pot[i][j], pot[nx][ny], col[i][j] != col[nx][ny]);
      }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      bfs(pot[i][j]);
      int temp = 0;
      for (int j = 1; j <= bk[0]; ++j) temp = max(temp, dis[bk[j]]);
      ans = min(ans, temp);
    }
  printf("%d", ans + 1);
  return 0;
}
