#include <bits/stdc++.h>
using namespace std;
const int N = 55;
char s[N][N];
int n, m, head[N * N], cnt, vis[N * N], id[N][N], dis[N * N];
struct edge {
  int next, to, w;
} e[N * N * 4];
void add(int u, int v, int w) {
  e[++cnt].next = head[u];
  e[cnt].to = v;
  e[cnt].w = w;
  head[u] = cnt;
}
void spfa(int s) {
  memset(vis, 0, sizeof vis);
  memset(dis, 0x3f, sizeof dis);
  vis[s] = true;
  dis[s] = 0;
  queue<int> q;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].to;
      if (dis[v] > dis[u] + e[i].w) {
        dis[v] = dis[u] + e[i].w;
        q.push(v);
        vis[v] = true;
      }
    }
    vis[u] = false;
  }
}
vector<int> black;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf(" %s", s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) id[i][j] = (i - 1) * m + j;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == 'B') black.push_back(id[i][j]);
      if (i < n)
        add(id[i][j], id[i + 1][j], s[i][j] != s[i + 1][j]),
            add(id[i + 1][j], id[i][j], s[i][j] != s[i + 1][j]);
      if (j < m)
        add(id[i][j], id[i][j + 1], s[i][j] != s[i][j + 1]),
            add(id[i][j + 1], id[i][j], s[i][j] != s[i][j + 1]);
    }
  }
  int siz = black.size();
  if (siz == 0) {
    puts("0");
    return 0;
  }
  int ans = n * m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      spfa(id[i][j]);
      int mx = 0;
      for (int k = 0; k < siz; k++) mx = max(mx, dis[black[k]]);
      ans = min(ans, mx + 1);
    }
  }
  cout << ans << endl;
  return 0;
}
