#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[105][105];
int p[105][105];
int g[2505][2505], gn[2505];
int color[2505];
int valid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
void dfs(int x, int y, int c) {
  p[x][y] = c;
  for (int i = 0; i < 4; i++)
    if (valid(x + dx[i], y + dy[i]) && p[x + dx[i]][y + dy[i]] == 0 &&
        a[x][y] == a[x + dx[i]][y + dy[i]])
      dfs(x + dx[i], y + dy[i], c);
}
int Q[2505] = {}, u[2502], ucs = 0, d[2505];
int go(int x) {
  ++ucs;
  int qb = 0, qe = 0;
  Q[qe++] = x;
  u[x] = ucs;
  d[x] = 0;
  while (qb < qe) {
    x = Q[qb++];
    for (int i = 0; i < gn[x]; i++) {
      if (u[g[x][i]] != ucs) {
        u[g[x][i]] = ucs;
        d[g[x][i]] = d[x] + 1;
        Q[qe++] = g[x][i];
      }
    }
  }
  return d[Q[qe - 1]] + color[Q[qe - 1]];
}
int main(void) {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", a[i]);
  int c = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (p[i][j] == 0) {
        ++c;
        color[c] = (a[i][j] == 'B');
        dfs(i, j, c);
      }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      for (int k = 0; k < 4; k++) {
        if (valid(i + dx[k], j + dy[k]) && p[i][j] != p[i + dx[k]][j + dy[k]]) {
          int A = p[i][j], B = p[i + dx[k]][j + dy[k]];
          g[A][gn[A]++] = B;
        }
      }
  int ans = 1000000;
  for (int i = 1; i <= c; i++) {
    ans = min(ans, go(i));
  }
  printf("%d\n", ans);
  return 0;
}
