#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[55][55];
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
vector<pair<int, int> > G[2555];
int vis[2555];
int bfs(int i, int j) {
  memset(vis, -1, sizeof vis);
  deque<pair<int, int> > q;
  q.push_front(make_pair(i * m + j, 0));
  int mx = 0;
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop_front();
    int u = cur.first, w = cur.second;
    vis[u] = w;
    if (a[u / m][u % m] == 'B') mx = w;
    for (int k = 0; k < (int)G[u].size(); k++) {
      int v = G[u][k].first, w2 = G[u][k].second;
      if (vis[v] == -1) {
        vis[v] = w + w2;
        if (w2 == 0)
          q.push_front(make_pair(v, w));
        else
          q.push_back(make_pair(v, w + w2));
      }
    }
  }
  return mx;
}
int main() {
  bool ok = 0;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", a[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] == 'B') ok = 1;
  if (!ok) {
    printf("0\n");
    return 0;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      for (int k = 0; k < 4; k++) {
        int newI = i + dx[k], newJ = j + dy[k];
        if (newI < 0 || newJ < 0 || newI >= n || newJ >= m) continue;
        if (a[i][j] == a[newI][newJ])
          G[i * m + j].push_back(make_pair(newI * m + newJ, 0));
        else
          G[i * m + j].push_back(make_pair(newI * m + newJ, 1));
      }
    }
  int mn = n * m + 1;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      mn = min(mn, bfs(i, j));
    }
  printf("%d\n", mn + 1);
  return 0;
}
