#include <bits/stdc++.h>
using namespace std;
const int C[4] = {0, 0, -1, 1};
const int H[4] = {-1, 1, 0, 0};
const int INF = 1e9 + 1;
const int N = 51;
int d[N][N];
char a[N][N];
int n, m;
int Solve(int U, int V) {
  deque<pair<int, int> > Q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) d[i][j] = INF;
  }
  d[U][V] = 0;
  Q.push_back(pair<int, int>(U, V));
  int res = -1;
  while (Q.size()) {
    int u = Q.front().first;
    int v = Q.front().second;
    Q.pop_front();
    if (a[u][v] == 'B') {
      res = max(res, d[u][v]);
    }
    for (int i = 0; i <= 3; i++) {
      int x = u + C[i];
      int y = v + H[i];
      if (x < 1 || x > n || y < 1 || y > m) continue;
      int w = 0;
      if (a[u][v] != a[x][y]) w = 1;
      if (d[u][v] + w < d[x][y]) {
        d[x][y] = d[u][v] + w;
        if (w == 0)
          Q.push_front(pair<int, int>(x, y));
        else
          Q.push_back(pair<int, int>(x, y));
      }
    }
  }
  return res + 1;
}
int main() {
  int res = INF;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      res = min(res, Solve(i, j));
    }
  }
  cout << res;
}
