#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
const int N = 505, M = 25005000, inf = 0x3f3f3f3f;
int n, m, num, dfn[N][N], dis[N];
bool mtr[2505][2505], v[2505];
char s[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int q[2505], head[2505], nxt[M], to[M], edgenum;
void addedge(int u, int v) {
  to[++edgenum] = v;
  nxt[edgenum] = head[u];
  head[u] = edgenum;
}
inline void dfs(int x, int y) {
  dfn[x][y] = num;
  for (register int i = 0; i <= 3; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) {
      if (s[nx][ny] == s[x][y] && !dfn[nx][ny]) dfs(nx, ny);
      if (s[nx][ny] != s[x][y] && dfn[nx][ny])
        mtr[num][dfn[nx][ny]] = mtr[dfn[nx][ny]][num] = 1;
    }
  }
}
inline int getdis(int s) {
  memset(dis, inf, sizeof(dis));
  int res = 0, f = 0, r = 1;
  q[f] = s;
  dis[s] = 0;
  while (f != r) {
    int u = q[f++];
    res = max(res, dis[u]);
    for (int i = head[u]; i != 0; i = nxt[i])
      if (dis[to[i]] > dis[u] + 1) {
        dis[to[i]] = dis[u] + 1;
        q[r++] = to[i];
      }
  }
  return res;
}
int main() {
  read(n);
  read(m);
  for (register int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (register int i = 1; i <= n; i++)
    for (register int j = 1; j <= m; j++)
      if (!dfn[i][j]) {
        ++num;
        v[num] = s[i][j] == 'B';
        dfs(i, j);
      }
  for (register int i = 1; i <= num; i++)
    for (register int j = 1; j <= num; j++)
      if (mtr[i][j]) addedge(i, j);
  int ans = 1e9;
  for (register int i = 1; i <= num; i++) {
    int d = getdis(i);
    ans = min(ans, d + (((d + v[i]) & 1) ? 1 : 0));
  }
  cout << ans;
  return 0;
}
