#include <bits/stdc++.h>
using namespace std;
int n, m, dx[]{-1, 0, 0, 1}, dy[]{0, -1, 1, 0};
vector<string> tab;
vector<vector<int>> dist;
priority_queue<pair<int, pair<int, int>>> PQ;
int dijkstra(int x, int y) {
  dist.assign(n, vector<int>(m, INT_MAX / 2));
  dist[x][y] = 1;
  PQ.push({0, {x, y}});
  while (!PQ.empty()) {
    int d = -PQ.top().first;
    x = PQ.top().second.first;
    y = PQ.top().second.second;
    PQ.pop();
    if (d > dist[x][y]) continue;
    for (int i = 0; i < 4; i++) {
      int x2 = x + dx[i], y2 = y + dy[i];
      if (x2 < 0 or x2 >= n or y2 < 0 or y2 >= m) continue;
      d = dist[x][y] + (tab[x][y] != tab[x2][y2]);
      if (dist[x2][y2] > d) {
        dist[x2][y2] = d;
        PQ.push({-d, {x2, y2}});
      }
    }
  }
  int maxi = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (tab[i][j] == 'B') maxi = max(maxi, dist[i][j]);
  return maxi;
}
int main() {
  cin >> n >> m;
  tab.resize(n);
  for (int i = 0; i < n; i++) cin >> tab[i];
  int ans = INT_MAX;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) ans = min(ans, dijkstra(i, j));
  printf("%d\n", ans);
  return 0;
}
