#include <bits/stdc++.h>
using namespace std;
const int x[] = {0, 0, 0, 1, -1};
const int y[] = {0, -1, 1, 0, 0};
int n, m, a[55][55], flag, ans = 0x3f3f3f3f;
char c;
vector<int> black;
int head[50005], cnt, dis[50005], vis[50005];
struct node {
  int next, to, dis;
} edge[50005];
inline void add(int from, int to, int dis) {
  edge[++cnt].next = head[from];
  edge[cnt].to = to;
  edge[cnt].dis = dis;
  head[from] = cnt;
}
inline void spfa(int x) {
  for (int i = 1; i <= n * m; i++) dis[i] = 0x3f3f3f3f;
  queue<int> q;
  dis[x] = 0;
  vis[x] = 1;
  q.push(x);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    vis[now] = 0;
    for (int i = head[now]; i; i = edge[i].next) {
      int to = edge[i].to;
      if (dis[to] > dis[now] + edge[i].dis) {
        dis[to] = dis[now] + edge[i].dis;
        if (!vis[to]) vis[to] = 1, q.push(to);
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> c;
      a[i][j] = (c == 'B');
      if (a[i][j]) flag = 1, black.push_back((i - 1) * m + j);
    }
  if (!flag) {
    cout << 0;
    return 0;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int k = 1; k <= 4; k++) {
        int xx = i + x[k], yy = j + y[k];
        if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
        add((i - 1) * m + j, (xx - 1) * m + yy, (a[i][j] != a[xx][yy]));
      }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      spfa((i - 1) * m + j);
      int tmp = 0;
      for (int k = 0; k < black.size(); k++) tmp = max(tmp, dis[black[k]]);
      ans = min(ans, tmp);
    }
  printf("%d", ans + 1);
  return 0;
}
