#include <bits/stdc++.h>
using namespace std;
char b[52][52];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  int n, m;
  cin >> n >> m;
  bool fl = 1;
  for (int i = 0; i < (int)n; ++i) {
    for (int j = 0; j < (int)m; ++j) {
      cin >> b[i][j];
      fl &= (b[i][j] == 'W');
    }
  }
  if (fl) {
    cout << 0 << endl;
    return 0;
  }
  int ans = 1e5;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      vector<int> ds(n * m + 1, 1e5);
      deque<int> q;
      vector<int> us(n * m + 1, 0);
      q.push_back(i * m + j);
      ds[i * m + j] = 0;
      while (!q.empty()) {
        int id = q.front();
        int x = q.front() / m;
        int y = q.front() % m;
        q.pop_front();
        int d = ds[id];
        if (us[id]) continue;
        us[id] = 1;
        for (int k = 0; k < 4; ++k) {
          int e = x + dx[k];
          int r = y + dy[k];
          int p = e * m + r;
          if (0 <= e && e < n && 0 <= r && r < m) {
            int l = (b[x][y] != b[e][r]);
            if (ds[p] > d + l) {
              ds[p] = d + l;
              if (l == 0)
                q.push_front(p);
              else
                q.push_back(p);
            }
          }
        }
      }
      (i), (j);
      ;
      int cans = 0;
      for (int e = 0; e < n; ++e) {
        for (int w = 0; w < m; ++w) {
          if (::b[e][w] == 'B') cans = max(cans, ds[e * m + w]);
        }
      }
      ans = min(cans, ans);
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      vector<int> usd;
    }
  }
  cout << ans + 1 << endl;
  if (0) cout << clock() << endl;
}
