#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char p = getchar();
  while (!isdigit(p)) {
    if (p == '-') f = -1;
    p = getchar();
  }
  while (isdigit(p)) x = (x << 3) + (x << 1) + (p ^ 48), p = getchar();
  return x * f;
}
const int maxn = 55;
int vec[2505], dx[5] = {0, -1, 1, 0, 0}, dy[5] = {0, 0, 0, -1, 1};
int n, m, a[maxn][maxn], dis[2505], s, ans = 1e9, cnt, v[2505];
int head[2505], ver[10005], nxt[10005], edge[10005];
inline int calc(int i, int j) { return (i - 1) * m + j; }
inline bool can(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m)
    return 0;
  else
    return 1;
}
inline void add(int x, int y, int z) {
  nxt[++cnt] = head[x];
  head[x] = cnt;
  ver[cnt] = y;
  edge[cnt] = z;
}
inline void dijkstra(int s) {
  memset(dis, 0x3f, sizeof(dis));
  memset(v, 0, sizeof(v));
  dis[s] = 0;
  priority_queue<pair<int, int> > q;
  q.push(make_pair(0, s));
  while (!q.empty()) {
    int x = q.top().second;
    q.pop();
    if (v[x]) continue;
    v[x] = 1;
    for (int i = head[x]; i; i = nxt[i]) {
      int y = ver[i];
      if (dis[y] > dis[x] + edge[i]) {
        dis[y] = dis[x] + edge[i];
        q.push(make_pair(-dis[y], y));
      }
    }
  }
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      char opt = getchar();
      while (opt != 'W' && opt != 'B') opt = getchar();
      if (opt == 'B')
        a[i][j] = 1, vec[++s] = calc(i, j);
      else
        a[i][j] = 0;
    }
  if (s == 0) {
    printf("0\n");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int k = 1; k <= 4; k++) {
        int nx = i + dx[k], ny = j + dy[k];
        if (!can(nx, ny)) continue;
        if (a[nx][ny] == a[i][j])
          add(calc(i, j), calc(nx, ny), 0);
        else
          add(calc(i, j), calc(nx, ny), 1);
      }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      dijkstra(calc(i, j));
      int tmp = 0;
      for (int k = 1; k <= s; k++) tmp = max(tmp, dis[vec[k]]);
      ans = min(ans, tmp);
    }
  cout << ans + 1 << endl;
  return 0;
}
