#include <bits/stdc++.h>
using namespace std;
const int maxn = 55;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {1, 0, 0, -1};
struct cc {
  int to, len;
};
vector<cc> ed[maxn * maxn];
queue<int> q;
char a[maxn][maxn];
int v[maxn * maxn], n, m, ans = 1e9, tot = 0, dis[maxn * maxn], id[maxn][maxn];
int spfa(int s) {
  for (int i = 1; i <= tot; i++) dis[i] = 1e9;
  v[s] = 1;
  q.push(s);
  dis[s] = 0;
  while (!q.empty()) {
    int k = q.front();
    q.pop();
    v[k] = 0;
    for (int i = 0; i < ed[k].size(); i++) {
      int y = ed[k][i].to, w = ed[k][i].len;
      if (dis[y] > dis[k] + w) {
        dis[y] = dis[k] + w;
        if (v[y] == 0) {
          v[y] = 1;
          q.push(y);
        }
      }
    }
  }
  int maxpath = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 'B') maxpath = max(maxpath, dis[id[i][j]] + 1);
  return maxpath;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      id[i][j] = ++tot;
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k <= 3; k++) {
        int x = i + dx[k], y = j + dy[k];
        if (x >= 1 && x <= n && y >= 1 && y <= m) {
          cc li1, li2;
          int id1 = id[i][j], id2 = id[x][y];
          li1.to = id[i][j];
          li2.to = id[x][y];
          if (a[i][j] == a[x][y])
            li1.len = li2.len = 0;
          else
            li1.len = li2.len = 1;
          ed[id1].push_back(li2);
          ed[id2].push_back(li1);
        }
      }
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) ans = min(ans, spfa(id[i][j]));
  printf("%d\n", ans);
  return 0;
}
