#include <bits/stdc++.h>
using namespace std;
const int maxn = 3005;
int m, n, ans, maxc, rc, fc;
vector<pair<int, int> > f[maxn];
string st;
int a[55][55], b[55][55];
int u[maxn * 100], dis[maxn];
int main() {
  cin >> m >> n;
  memset(a, 0, sizeof(a));
  for (int i = 1; i <= m; i++) {
    cin >> st;
    for (int j = 1; j <= n; j++)
      if (st[j - 1] == 'B')
        a[i][j] = 1;
      else
        a[i][j] = 0;
  }
  memset(f, 0, sizeof(f));
  memset(b, 0, sizeof(b));
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) b[i][j] = (i - 1) * n + j;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      if (i > 1) {
        if (a[i][j] == a[i - 1][j])
          f[b[i][j]].push_back(make_pair(b[i - 1][j], 0));
        else
          f[b[i][j]].push_back(make_pair(b[i - 1][j], 1));
      }
      if (j > 1) {
        if (a[i][j] == a[i][j - 1])
          f[b[i][j]].push_back(make_pair(b[i][j - 1], 0));
        else
          f[b[i][j]].push_back(make_pair(b[i][j - 1], 1));
      }
      if (i < m) {
        if (a[i][j] == a[i + 1][j])
          f[b[i][j]].push_back(make_pair(b[i + 1][j], 0));
        else
          f[b[i][j]].push_back(make_pair(b[i + 1][j], 1));
      }
      if (j < n) {
        if (a[i][j] == a[i][j + 1])
          f[b[i][j]].push_back(make_pair(b[i][j + 1], 0));
        else
          f[b[i][j]].push_back(make_pair(b[i][j + 1], 1));
      }
    }
  ans = 2147483647;
  for (int i = 1; i <= m * n; i++) {
    memset(dis, 127, sizeof(dis));
    dis[i] = 0;
    rc = 1, fc = 1, u[rc] = i;
    while (rc <= fc) {
      for (int j = 0; j < f[u[rc]].size(); j++)
        if (dis[u[rc]] + f[u[rc]][j].second < dis[f[u[rc]][j].first]) {
          dis[f[u[rc]][j].first] = dis[u[rc]] + f[u[rc]][j].second;
          ++fc, u[fc] = f[u[rc]][j].first;
        }
      ++rc;
    }
    maxc = 0;
    for (int j = 1; j <= m; j++)
      for (int k = 1; k <= n; k++)
        if (a[j][k] == 1) maxc = max(maxc, dis[b[j][k]] + 1);
    ans = min(ans, maxc);
  }
  cout << ans << endl;
  return 0;
}
