#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2600;
int n, m;
char s[60][60];
int addedge[MAXN][MAXN];
int blocks[60][60];
int cnt;
int dis[MAXN];
bool inside(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
vector<int> B;
vector<int> e[MAXN];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int MAXLEN = 0;
int color[MAXN];
void bfs(int x, int y, char st) {
  ++cnt;
  if (st == 'W')
    color[cnt] = 1;
  else
    color[cnt] = 0;
  if (st == 'B') B.push_back(cnt);
  queue<pair<int, int> > q;
  blocks[x][y] = cnt;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    int lx = q.front().first, ly = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = lx + dx[i], ny = ly + dy[i];
      if (inside(nx, ny) && s[nx][ny] == st && !blocks[nx][ny]) {
        blocks[nx][ny] = cnt;
        q.push(make_pair(nx, ny));
      }
    }
  }
}
void spfa(int st) {
  for (int i = 1; i <= cnt; i++) dis[i] = 0x3F3F3F3F;
  dis[st] = 0;
  queue<int> q;
  q.push(st);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < e[u].size(); i++) {
      int v = e[u][i];
      if (dis[v] > dis[u] + 1) {
        dis[v] = dis[u] + 1;
        q.push(v);
      }
    }
  }
  int MX = 0;
  for (int i = 0; i < B.size(); i++) {
    if (dis[B[i]] > MX) MX = dis[B[i]];
  }
  MAXLEN = min(MAXLEN, MX + 1);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", s[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (!blocks[i][j]) {
        bfs(i, j, s[i][j]);
      }
  for (int x = 0; x < n; x++)
    for (int y = 0; y < m; y++) {
      for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (inside(nx, ny) && blocks[nx][ny] != blocks[x][y]) {
          {
            int u = blocks[x][y], v = blocks[nx][ny];
            if (addedge[u][v]) continue;
            e[u].push_back(v);
            addedge[u][v] = true;
          }
        }
      }
    }
  if (B.size() == 0)
    puts("0");
  else {
    MAXLEN = 0x3F3F3F3F;
    for (int i = 1; i <= cnt; i++) spfa(i);
    printf("%d\n", MAXLEN);
  }
  return 0;
}
