#include <bits/stdc++.h>
using namespace std;
char s[60][60];
class Solution {
  int N, M;
  int dis[50][50], vis[50][50], visn = 1;
  int bfs(int x, int y) {
    deque<pair<int, int>> q;
    q.emplace_back(x, y);
    dis[x][y] = 0;
    ++visn;
    vis[x][y] = visn;
    const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
    int ans = 0;
    while (q.size()) {
      tie(x, y) = q.front();
      q.pop_front();
      ans = max(ans, dis[x][y]);
      for (int d = 0; d < 4; ++d) {
        int nx = x + dx[d], ny = y + dy[d];
        if (nx < 0 or ny < 0 or nx >= N or ny >= M) {
          ans = max(ans, dis[x][y] + (s[x][y] == 'B'));
          continue;
        }
        if (vis[nx][ny] == visn)
          continue;
        else {
          if (s[x][y] != s[nx][ny]) {
            dis[nx][ny] = dis[x][y] + 1;
            vis[nx][ny] = visn;
            q.emplace_back(nx, ny);
          } else {
            dis[nx][ny] = dis[x][y];
            vis[nx][ny] = visn;
            q.emplace_front(nx, ny);
          }
        }
      }
    }
    return ans;
  }

 public:
  void run() {
    int ans = 1e9;
    cin >> N >> M;
    for (int i = 0; i < N; ++i) cin >> s[i];
    for (int i = 0; i < N; ++i)
      for (int j = 0; j < M; ++j) {
        ans = min(ans, bfs(i, j));
      }
    cout << ans << '\n';
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  Solution().run();
  return 0;
}
