#include <bits/stdc++.h>
using std::deque;
int n = 0, m = 0;
char map[53][53];
const int dx[4] = {-1, +1, 0, 0}, dy[4] = {0, 0, +1, -1};
int dis[53][53], in[53][53];
struct Pos {
  int y, x;
};
int spfa(int y, int x) {
  memset(dis, 0x3f, sizeof dis);
  memset(in, 0, sizeof in);
  dis[y][x] = 0;
  deque<Pos> q;
  q.push_back({y, x});
  in[y][x] = true;
  while (!q.empty()) {
    Pos p = q.front();
    q.pop_front();
    in[p.y][p.x] = false;
    for (int i = 0; i < 4; ++i) {
      Pos t = Pos{p.y + dy[i], p.x + dx[i]};
      if (t.x >= 1 && t.x <= m && t.y >= 1 && t.y <= n) {
        if (dis[t.y][t.x] > dis[p.y][p.x] + (map[p.y][p.x] != map[t.y][t.x])) {
          dis[t.y][t.x] = dis[p.y][p.x] + (map[p.y][p.x] != map[t.y][t.x]);
          if (!in[t.y][t.x]) {
            if (map[p.y][p.x] == map[t.y][t.x])
              q.push_front(t);
            else
              q.push_back(t);
            in[t.y][t.x] = true;
          }
        }
      }
    }
  }
  int maxBlackV = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (map[i][j] == 'B' && dis[i][j] > maxBlackV) maxBlackV = dis[i][j];
  return maxBlackV + 1;
}
inline int min(int a, int b) { return a < b ? a : b; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%s", map[i] + 1);
  bool isBlock = false;
  for (int i = 1; i <= n && !isBlock; ++i)
    for (int j = 1; j <= m && !isBlock; ++j) isBlock |= (map[i][j] == 'B');
  if (!isBlock) {
    printf("0");
    return 0;
  }
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) ans = min(ans, spfa(i, j));
  printf("%d", ans);
  return 0;
}
