#include <bits/stdc++.h>
using namespace std;
int n, m;
inline int get_bh(int x, int y) { return (x - 1) * m + y; }
char c[51][51];
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}, first[2505], ne[80005],
    to[80005], val[80005], cnt = 0;
inline void add(int x, int y, int c) {
  cnt++;
  ne[cnt] = first[x];
  first[x] = cnt;
  to[cnt] = y;
  val[cnt] = c;
}
int gp = 0, dis[2505], dan = 2147483647, guizi;
priority_queue<pair<int, int> > q;
inline void gun(int s) {
  q.push(make_pair(0, s));
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    for (int i = first[u]; i; i = ne[i]) {
      int v = to[i];
      if (dis[v] > dis[u] + val[i]) {
        dis[v] = dis[u] + val[i];
        q.push(make_pair(-dis[v], v));
      }
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (c[i][j] == 'B') {
        int laozi = get_bh(i, j);
        if (dis[laozi] > guizi) guizi = dis[laozi];
      }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'B') gp++;
    }
  if (gp == 0) {
    cout << "0";
    return 0;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int s1 = get_bh(i, j);
      for (int k = 0; k <= 3; k++) {
        int tx = i + dir[k][0], ty = j + dir[k][1];
        if (tx >= 1 && tx <= n && ty >= 1 && ty <= m) {
          int s2 = get_bh(tx, ty);
          if (c[tx][ty] == c[i][j])
            add(s1, s2, 0), add(s2, s1, 0);
          else
            add(s1, s2, 1), add(s2, s1, 1);
        }
      }
    }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      guizi = 0;
      int s = get_bh(i, j);
      memset(dis, 63, sizeof(dis));
      dis[s] = 0;
      gun(s);
      if (guizi + 1 < dan) dan = guizi + 1;
    }
  cout << dan;
  return 0;
}
