#include <bits/stdc++.h>
using namespace std;
const int N = 55;
int n, m, ans, d[N][N];
char col[N][N];
bool vis[N][N];
queue<pair<int, int> > q;
int dx[] = {1, 0, 0, -1}, dy[] = {0, 1, -1, 0};
void bfs(int i, int j) {
  q.push(make_pair(i, j));
  memset(d, 63, sizeof(d));
  memset(vis, 0, sizeof(vis));
  d[i][j] = 1;
  vis[i][j] = 1;
  int re = 0;
  while (!q.empty()) {
    pair<int, int> z = q.front();
    q.pop();
    int x = z.first, y = z.second;
    for (int d0 = 0; d0 < 4; ++d0) {
      int tx = x + dx[d0], ty = y + dy[d0];
      if ((tx > n) || (!tx) || (ty > m) || (!ty)) continue;
      if (d[tx][ty] > d[x][y] + (col[x][y] != col[tx][ty])) {
        d[tx][ty] = d[x][y] + (col[x][y] != col[tx][ty]);
        if (!vis[tx][ty]) vis[tx][ty] = 1, q.push(make_pair(tx, ty));
      }
    }
    vis[x][y] = 0;
  }
  for (int x = 1; x <= n; ++x)
    for (int y = 1; y <= m; ++y) {
      re = max(re, d[x][y] - (col[x][y] == 'W'));
    }
  ans = min(re, ans);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%s", col[i] + 1);
  ans = 500006;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) bfs(i, j);
  printf("%d\n", ans);
  return 0;
}
