#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int maxn = 3e5 + 5;
int tox[] = {0, 0, 1, -1};
int toy[] = {1, -1, 0, 0};
char g[55][55];
int n, m, tot, dist[55][55];
bool judge(int x, int y) {
  if (x < 0 || x >= n) return false;
  if (y < 0 || y >= m) return false;
  return true;
}
struct node {
  int x, y, t;
  bool operator<(const node& nd) const { return t > nd.t; }
};
int dijk(int x, int y) {
  int ret = 0, t, cnt = 0;
  priority_queue<node> q;
  node now;
  now.t = 1, now.x = x, now.y = y;
  q.push(now);
  dist[x][y] = 1;
  while (!q.empty()) {
    now = q.top();
    q.pop();
    if (++cnt >= tot) break;
    for (int i = 0; i < 4; i++) {
      int tx = now.x + tox[i], ty = now.y + toy[i], tt = now.t;
      if (!judge(tx, ty)) continue;
      if (g[tx][ty] != g[now.x][now.y]) ++tt;
      if (tt >= dist[tx][ty]) continue;
      node nxt;
      nxt.x = tx, nxt.y = ty, nxt.t = tt;
      dist[tx][ty] = tt;
      q.push(nxt);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (g[i][j] == 'W') continue;
      if (dist[i][j] > ret) ret = dist[i][j];
    }
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  tot = n * m;
  bool ok = true;
  for (int i = 0; i < n; i++) {
    scanf("%s", g[i]);
    for (int j = 0; j < m; j++) {
      if (g[i][j] == 'W') continue;
      ok = false;
    }
  }
  if (ok) {
    puts("0");
    return 0;
  }
  int ans = 0x3f3f3f3f;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      for (int ii = 0; ii < n; ii++)
        for (int jj = 0; jj < m; jj++) dist[ii][jj] = 0x3f3f3f3f;
      int now = dijk(i, j);
      if (now < ans) ans = now;
    }
  }
  printf("%d\n", (ans));
  return 0;
}
