#include <bits/stdc++.h>
using namespace std;
int n, m;
int dist[52][52];
int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
char s[52][52];
bool inrange(int x, int y) { return x >= 1 && y >= 1 && x <= m && y <= n; }
int f(int sy, int sx) {
  int i, j, ret = 0;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) dist[i][j] = 1e6;
  deque<pair<int, pair<int, int>>> dq;
  dist[sy][sx] = 0;
  dq.push_back({dist[sy][sx], {sx, sy}});
  while (!dq.empty()) {
    int w = dq.front().first;
    int x = dq.front().second.first, y = dq.front().second.second;
    dq.pop_front();
    if (dist[y][x] < w) continue;
    ret = max(ret, dist[y][x]);
    for (int i = 0; i < 4; i++) {
      int tx = x + dir[i][0];
      int ty = y + dir[i][1];
      if (!inrange(tx, ty)) continue;
      int c = s[y][x] != s[ty][tx];
      int tw = dist[y][x] + c;
      if (tw < dist[ty][tx]) {
        dist[ty][tx] = tw;
        if (c == 1)
          dq.push_back({tw, {tx, ty}});
        else
          dq.push_front({tw, {tx, ty}});
      }
    }
  }
  if (s[sy][sx] == 'W') {
    if (ret % 2) ret++;
  } else {
    if (ret % 2 == 0) ret++;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int i, j;
  cin >> n >> m;
  for (i = 1; i <= n; i++) cin >> s[i] + 1;
  int ans = 1e6, wc = 0;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      ans = min(ans, f(i, j));
    }
  }
  cout << ans;
  return 0;
}
