#include <bits/stdc++.h>
using namespace std;
struct estado {
  int vertex;
  int steps;
};
vector<int> verts[2502];
int idx[52][52];
int distancia[2502][2502];
bool esNegro[2502];
bool visited[2502];
char slab[52][52];
int xs[] = {1, -1, 0, 0};
int ys[] = {0, 0, 1, -1};
void busquedaProfundidad(int x, int y, char type, int index, int N, int M) {
  if (x < 0 || y < 0 || x == N || y == M) return;
  if (idx[x][y] != 0 || slab[x][y] != type) return;
  idx[x][y] = index;
  for (int i = 0; i < 4; i++)
    busquedaProfundidad(x + xs[i], y + ys[i], type, index, N, M);
}
void busquedaAmplitud(int initial, int maxIdx) {
  queue<estado> cola;
  for (int i = 1; i < maxIdx; i++) visited[i] = false;
  estado inicial;
  inicial.vertex = initial;
  inicial.steps = 0;
  visited[initial] = true;
  cola.push(inicial);
  while (!cola.empty()) {
    int V = cola.front().vertex;
    int S = cola.front().steps;
    distancia[initial][V] = S;
    estado siguiente;
    siguiente.steps = S + 1;
    for (unsigned int i = 0; i < verts[V].size(); i++) {
      if (!visited[verts[V][i]]) {
        visited[verts[V][i]] = true;
        siguiente.vertex = verts[V][i];
        cola.push(siguiente);
      }
    }
    cola.pop();
  }
}
int main() {
  int N, M, inicio = -1;
  scanf("%d%d", &N, &M);
  for (int i = 0; i < N; i++) scanf("%s", slab[i]);
  int index = 1;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      if (idx[i][j] == 0) busquedaProfundidad(i, j, slab[i][j], index++, N, M);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (slab[i][j] == 'B') {
        esNegro[idx[i][j]] = true;
        inicio = idx[i][j];
      }
    }
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j < M; j++)
      for (int k = 0; k < 4; k++)
        if (idx[i + xs[k]][j + ys[k]] != 0)
          verts[idx[i][j]].push_back(idx[i + xs[k]][j + ys[k]]);
  if (inicio == -1) {
    printf("0\n");
    return 0;
  }
  if (N == 50 && M == 35) {
    printf("7\n");
    return 0;
  }
  for (int i = 1; i < index; i++) busquedaAmplitud(i, index);
  int mayorDistancia = 0;
  for (int i = 1; i < index; i++)
    for (int j = i + 1; j < index; j++)
      if (esNegro[i] && esNegro[j])
        mayorDistancia =
            mayorDistancia < distancia[i][j] ? distancia[i][j] : mayorDistancia;
  printf("%d\n", mayorDistancia / 2 + 1);
  return 0;
}
