#include <bits/stdc++.h>
const int N = 55;
int n, m;
int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
int e_cnt, heads[7200], dis[7200];
bool vis[7200], col[7200];
char s[N][N];
struct Edge {
  int v, nxt, w;
} e[(7200) << 1];
inline void add(int u, int v, int w) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].w = w;
}
int spfa(int s) {
  int res = 0;
  std::queue<int> q;
  memset(dis, 0x3f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  q.push(s), vis[s] = true;
  dis[s] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = false;
    for (int i = heads[u]; i; i = e[i].nxt) {
      int v = e[i].v;
      if (dis[v] > dis[u] + e[i].w) {
        dis[v] = dis[u] + e[i].w;
        if (!vis[v]) {
          q.push(v);
          vis[v] = true;
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (col[(i - 1) * m + j]) res = std::max(res, dis[(i - 1) * m + j]);
    }
  }
  return res;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s[i] + 1);
  }
  bool flag = false;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i][j] == 'B') flag = true;
      col[(i - 1) * m + j] = s[i][j] == 'B' ? true : false;
      for (int k = 0; k < 4; ++k) {
        int tx = i + dx[k], ty = j + dy[k];
        if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
        if (s[tx][ty] == s[i][j]) add((i - 1) * m + j, (tx - 1) * m + ty, 0);
        if (s[tx][ty] != s[i][j]) add((i - 1) * m + j, (tx - 1) * m + ty, 1);
      }
    }
  }
  if (flag == false)
    printf("0");
  else {
    int ans = 0x3fffffff;
    for (int i = 1; i <= m * n; ++i) {
      ans = std::min(ans, spfa(i));
    }
    printf("%d", ans);
  }
  return 0;
}
