#include <bits/stdc++.h>
#pragma comment(linker, "/stack:64000000")
using namespace std;
template <typename first>
inline first abs(const first& a) {
  return a < 0 ? -a : a;
}
template <typename first>
inline first sqr(const first& a) {
  return a * a;
}
const int INF = INT_MAX / 2;
const long double EPS = 1e-9;
const long double PI = 3.1415926535897932384626433832795;
const int N = 50 + 13;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
char a[N][N];
int n, m, d[N][N];
inline int calc(int x, int y) {
  memset(d, 63, sizeof(d));
  queue<pair<int, int> > q;
  q.push(make_pair((x), (y)));
  d[x][y] = 0;
  while (!q.empty()) {
    pair<int, int> cur = q.front();
    q.pop();
    int x = cur.first;
    int y = cur.second;
    for (int i = 0; i < int(4); i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if ((0 <= (nx) && (nx) < (n) && 0 <= (ny) && (ny) < (m)) &&
          d[nx][ny] > d[x][y] + (a[x][y] != a[nx][ny])) {
        d[nx][ny] = d[x][y] + (a[x][y] != a[nx][ny]);
        q.push(make_pair((nx), (ny)));
      }
    }
  }
  int ans = -1;
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++)
      if (a[i][j] == 'B') ans = max(ans, d[i][j]);
  return ans;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++) cin >> a[i][j];
  int ans = INF;
  for (int i = 0; i < int(n); i++)
    for (int j = 0; j < int(m); j++) ans = min(ans, calc(i, j));
  if (ans > INF / 2) ans = ans;
  cout << ans + 1;
  return 0;
}
