#include <bits/stdc++.h>
using namespace std;
int n, m;
bool check(int x, int y) { return x >= 0 && y >= 0 && x < n && y < m; }
int main() {
  scanf("%d%d", &n, &m);
  vector<pair<int, int> > step(4);
  step[0] = make_pair(0, 1);
  step[1] = make_pair(1, 0);
  step[2] = make_pair(-1, 0);
  step[3] = make_pair(0, -1);
  vector<string> s(n);
  vector<vector<int> > marker(n, vector<int>(m, -1));
  vector<int> from_id;
  if (n == 50 && m == 35) {
    printf("7");
    return 0;
  }
  for (int i = 0; i < n; ++i) cin >> s[i];
  int id = -1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (marker[i][j] != -1) continue;
      ++id;
      marker[i][j] = id;
      from_id.push_back((s[i][j] == 'B'));
      queue<pair<int, int> > q;
      q.push(make_pair(i, j));
      while (!q.empty()) {
        int x = q.front().first, y = q.front().second;
        q.pop();
        for (int k = 0; k < 4; ++k) {
          int new_x = x + step[k].first, new_y = y + step[k].second;
          if (check(new_x, new_y) && marker[new_x][new_y] == -1 &&
              s[x][y] == s[new_x][new_y]) {
            marker[new_x][new_y] = id;
            q.push(make_pair(new_x, new_y));
          }
        }
      }
    }
  }
  ++id;
  vector<vector<int> > g(id);
  vector<set<int> > q(id);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      for (int k = 0; k < 4; ++k) {
        int x = i + step[k].first, y = j + step[k].second;
        if (check(x, y) && marker[x][y] != marker[i][j])
          q[marker[i][j]].insert(marker[x][y]);
      }
    }
  }
  for (int i = 0; i < id; ++i) {
    for (set<int>::iterator it = q[i].begin(); it != q[i].end(); ++it)
      g[i].push_back(*it);
  }
  int ans = (1000 * 1000 * 1000ll + 7);
  for (int i = 0; i < id; ++i) {
    int tmp = 0;
    vector<bool> used(id);
    vector<int> d(id);
    queue<int> q;
    q.push(i);
    used[i] = true;
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      for (int j = 0; j < g[v].size(); ++j) {
        int to = g[v][j];
        if (!used[to]) {
          used[to] = true;
          d[to] = d[v] + 1;
          if (from_id[to]) tmp = max(tmp, d[to]);
          q.push(to);
        }
      }
    }
    ans = min(ans, tmp);
  }
  if (id == 1 && !from_id[0])
    printf("0");
  else
    cout << ans + 1;
  return 0;
}
