#include <bits/stdc++.h>
using namespace std;
vector<int> G[52 * 52];
char s[52][52];
int a[52][52], num[52][52], C[52 * 52], d[52 * 52];
bool vis[52 * 52];
int N, M, n;
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
bool in(int x, int y) { return x >= 0 && x < N && y >= 0 && y < M; }
void dfs(int x, int y, int id, int color) {
  num[x][y] = id;
  for (int k = 0; k < 4; k++) {
    int nx = x + dx[k];
    int ny = y + dy[k];
    if (in(nx, ny) && a[nx][ny] == color && num[nx][ny] == 0) {
      dfs(nx, ny, id, color);
    }
  }
}
void bfs(int s) {
  queue<int> q;
  memset(vis, 0, sizeof(vis));
  d[s] = 0;
  vis[s] = 1;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (!vis[v]) {
        d[v] = d[u] + 1;
        vis[v] = 1;
        q.push(v);
      }
    }
  }
}
int main() {
  scanf("%d%d", &N, &M);
  bool ok = 0;
  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 (s[i][j] == 'B')
        a[i][j] = 1, ok = 1;
      else
        a[i][j] = 0;
  if (ok == 0) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      if (num[i][j] == 0) {
        n++;
        dfs(i, j, n, a[i][j]);
        C[num[i][j]] = a[i][j];
      }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      for (int k = 0; k < 4; k++) {
        int ni = i + dx[k];
        int nj = j + dy[k];
        if (in(ni, nj)) {
          G[num[i][j]].push_back(num[ni][nj]);
          G[num[ni][nj]].push_back(num[i][j]);
        }
      }
    }
  }
  int ans = 1e9;
  for (int i = 1; i <= n; i++) {
    int cur = 0;
    if (1) {
      bfs(i);
      for (int j = 1; j <= n; j++) {
        if (C[j] == 1 && vis[j] == 1) cur = max(cur, d[j]);
      }
    }
    ans = min(ans, cur);
  }
  printf("%d\n", ans + 1);
  return 0;
}
