#include <bits/stdc++.h>
using namespace std;
vector<vector<bool>> slab;
int solve(int x, int y) {
  int n = slab.size(), m = slab[0].size();
  vector<vector<int>> dist(n, vector<int>(m, -1));
  dist[x][y] = 0;
  deque<pair<int, int>> q;
  q.push_back(make_pair(x, y));
  int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
  int ret = 0;
  while (!q.empty()) {
    pair<int, int> top = q.front();
    q.pop_front();
    for (int i = 0; i < 4; ++i) {
      int xx = top.first + dx[i], yy = top.second + dy[i];
      if (xx >= 0 && xx < n && yy >= 0 && yy < m && dist[xx][yy] == -1) {
        if (slab[xx][yy] == slab[top.first][top.second]) {
          dist[xx][yy] = dist[top.first][top.second];
          q.push_front(make_pair(xx, yy));
        } else {
          dist[xx][yy] = dist[top.first][top.second] + 1;
          q.push_back(make_pair(xx, yy));
        }
        if (slab[xx][yy]) ret = max(ret, dist[xx][yy]);
      }
    }
  }
  return ret;
}
int main() {
  int n, m;
  cin >> n >> m;
  slab.assign(n, vector<bool>(m, false));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      char tmp;
      cin >> tmp;
      if (tmp == 'B') slab[i][j] = true;
    }
  int ans = n * m;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) ans = min(ans, solve(i, j));
  if (ans == 0) {
    bool flag = true;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j)
        if (slab[i][j]) {
          flag = false;
          break;
        }
    if (flag) ans = -1;
  }
  ++ans;
  cout << ans << endl;
  return 0;
}
