#include <bits/stdc++.h>
using namespace std;
int n, m, d[10000], ans = INT_MAX, cnt;
char s[55][55];
bool vis[10000];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%s", s[i]);
  bool flag = false;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'B') flag = true;
  if (!flag) {
    return 0 * printf("0\n");
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cnt = 0;
      memset(vis, 0, sizeof vis);
      for (int k = 0; k < 6000; k++) d[k] = 1e9;
      queue<pair<int, int> > q[2];
      q[0].push({0, i * 100 + j});
      while (!q[0].empty()) {
        while (!q[0].empty()) {
          int dist = -q[0].front().first, x = q[0].front().second;
          q[0].pop();
          if (vis[x]) continue;
          vis[x] = true;
          d[x] = dist;
          if (s[x / 100][x % 100] == 'B') cnt = max(cnt, dist);
          if (x / 100 + 1 < n)
            q[s[x / 100][x % 100] != s[x / 100 + 1][x % 100]].push(
                {-dist - (s[x / 100][x % 100] != s[x / 100 + 1][x % 100]),
                 x + 100});
          if (x % 100 + 1 < m)
            q[s[x / 100][x % 100] != s[x / 100][x % 100 + 1]].push(
                {-dist - (s[x / 100][x % 100] != s[x / 100][x % 100 + 1]),
                 x + 1});
          if (x / 100 - 1 >= 0)
            q[s[x / 100][x % 100] != s[x / 100 - 1][x % 100]].push(
                {-dist - (s[x / 100][x % 100] != s[x / 100 - 1][x % 100]),
                 x - 100});
          if (x % 100 - 1 >= 0)
            q[s[x / 100][x % 100] != s[x / 100][x % 100 - 1]].push(
                {-dist - (s[x / 100][x % 100] != s[x / 100][x % 100 - 1]),
                 x - 1});
        }
        q[0] = q[1];
        while (!q[1].empty()) q[1].pop();
      }
      ans = min(ans, cnt + 1);
    }
  }
  printf("%d\n", ans);
  return 0;
}
