#include <bits/stdc++.h>
using namespace std;
int N, M;
string line[100];
vector<vector<int> > graph;
vector<vector<int> > weight;
int Ns;
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
const int INFI = 99999999;
vector<int> dijkstra(int start) {
  vector<int> dist(Ns, INFI);
  vector<bool> caled(Ns, false);
  dist[start] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  q.push(make_pair(0, start));
  while (q.size() > 0) {
    pair<int, int> now = q.top();
    q.pop();
    int nowDist = now.first;
    int nowID = now.second;
    if (caled[nowID]) continue;
    caled[nowID] = true;
    for (int i = 0; i < graph[nowID].size(); ++i) {
      int nextID = graph[nowID][i];
      int w = weight[nowID][i];
      if (nowDist + w < dist[nextID]) {
        dist[nextID] = nowDist + w;
        q.push(make_pair(dist[nextID], nextID));
      }
    }
  }
  return dist;
}
void deal() {
  int blacks = 0;
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < M; ++j)
      if (line[i][j] == 'B') blacks++;
  if (blacks == 0) {
    cout << 0 << endl;
    return;
  }
  int ans = INFI;
  for (int i = 0; i < Ns; ++i) {
    vector<int> dists = dijkstra(i);
    int maxDis = 0;
    for (int j = 0; j < Ns; ++j) {
      int x = j / M;
      int y = j % M;
      if (line[x][y] == 'B') maxDis = max(maxDis, dists[j]);
    }
    ans = min(ans, maxDis);
  }
  cout << ans + 1 << endl;
}
void init() {
  scanf("%d%d", &N, &M);
  getline(cin, line[0]);
  for (int i = 0; i < N; ++i) getline(cin, line[i]);
  Ns = N * M;
  graph.resize(Ns);
  weight.resize(Ns);
  for (int i = 0; i < N; ++i)
    for (int j = 0; j < M; ++j) {
      int nowID = i * M + j;
      for (int k = 0; k < 4; ++k) {
        int ni = i + dir[k][0];
        int nj = j + dir[k][1];
        if (ni >= 0 && ni < N && nj >= 0 && nj < M) {
          int newID = ni * M + nj;
          graph[nowID].push_back(newID);
          if (line[i][j] == line[ni][nj])
            weight[nowID].push_back(0);
          else
            weight[nowID].push_back(1);
        }
      }
    }
}
int main() {
  init();
  deal();
  return 0;
}
