#include <bits/stdc++.h>
using namespace std;
inline long long getint() {
  long long _x = 0, _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
  if (_tc == '-') _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x * _tmp;
}
long long mypow(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 1ll;
  long long _tmp = mypow(_a, _x / 2, _mod);
  _tmp = (_tmp * _tmp) % _mod;
  if (_x & 1) _tmp = (_tmp * _a) % _mod;
  return _tmp;
}
inline long long add(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x + _y;
  if (_ >= _mod) _ -= _mod;
  return _;
}
inline long long sub(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x - _y;
  if (_ < 0) _ += _mod;
  return _;
}
inline long long mul(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x * _y;
  if (_ >= _mod) _ %= _mod;
  return _;
}
inline bool equal(double _x, double _y) {
  return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
int n, m;
char c[55][55];
bool in(int xn, int xm) { return 0 <= xn && xn < n && 0 <= xm && xm < m; }
inline int trans(int ii, int jj) { return ii * m + jj; }
void build() {}
void init() {
  n = getint();
  m = getint();
  for (int i = 0; i < n; i++) scanf("%s", c[i]);
}
int di[] = {0, 0, 1, -1};
int dj[] = {1, -1, 0, 0};
int dst[55 * 55][55 * 55];
bool got[55 * 55][55 * 55];
deque<pair<int, int> > dq;
void BFS(int ni, int nj) {
  dst[trans(ni, nj)][trans(ni, nj)] = 0;
  got[trans(ni, nj)][trans(ni, nj)] = true;
  dq.push_back(make_pair(ni, nj));
  while (dq.size()) {
    pair<int, int> tp = dq.front();
    dq.pop_front();
    int xi = tp.first, xj = tp.second;
    for (int dir = 0; dir < 4; dir++) {
      int nxti = xi + di[dir];
      int nxtj = xj + dj[dir];
      if (in(nxti, nxtj) && !got[trans(ni, nj)][trans(nxti, nxtj)]) {
        got[trans(ni, nj)][trans(nxti, nxtj)] = true;
        if (c[xi][xj] == c[nxti][nxtj]) {
          dst[trans(ni, nj)][trans(nxti, nxtj)] =
              dst[trans(ni, nj)][trans(xi, xj)];
          dq.push_front(make_pair(nxti, nxtj));
        } else {
          dst[trans(ni, nj)][trans(nxti, nxtj)] =
              dst[trans(ni, nj)][trans(xi, xj)] + 1;
          dq.push_back(make_pair(nxti, nxtj));
        }
      }
    }
  }
}
void solve() {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) BFS(i, j);
  int ans = n * m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      int tans = -1;
      for (int ii = 0; ii < n; ii++)
        for (int jj = 0; jj < m; jj++)
          if (c[ii][jj] == 'B')
            tans = max(tans, dst[trans(i, j)][trans(ii, jj)]);
      ans = min(ans, tans + 1);
    }
  cout << ans << endl;
}
int main() {
  build();
  while (__--) {
    init();
    solve();
  }
}
