#include <bits/stdc++.h>
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, -1, 1};
inline bool is_valid(int r, int c, int R, int C) {
  return (0 <= r && r < R && 0 <= c && c < C);
}
int main(void) {
  int R, C;
  std::cin >> R >> C;
  std::vector<std::string> map(R);
  for (int r = 0; r < R; ++r) {
    std::cin >> map[r];
  }
  int n = R * C;
  std::vector<std::vector<std::pair<int, int>>> G(n);
  for (int r = 0; r < R; ++r) {
    for (int c = 0; c < C; ++c) {
      for (int d = 0; d < 4; ++d) {
        int nr = r + dr[d];
        int nc = c + dc[d];
        if (is_valid(nr, nc, R, C)) {
          int u = r * C + c;
          int v = nr * C + nc;
          int cost = (map[r][c] != map[nr][nc]);
          G[u].emplace_back(v, cost);
          G[v].emplace_back(u, cost);
        }
      }
    }
  }
  int min = INT_MAX;
  for (int k = 0; k < n; ++k) {
    std::vector<int> d(n, INT_MAX);
    std::priority_queue<std::pair<int, int>> q;
    d[k] = 0;
    q.push({0, k});
    int max = -1;
    while (!q.empty()) {
      int u = q.top().second;
      q.pop();
      int r = u / C;
      int c = u % C;
      if (map[r][c] == 'B') {
        max = std::max(max, d[u]);
      }
      for (auto& cv : G[u]) {
        int v = cv.first;
        int cost = cv.second;
        if (d[v] > d[u] + cost) {
          d[v] = d[u] + cost;
          q.push({-d[v], v});
        }
      }
    }
    min = std::min(max, min);
  }
  std::cout << min + 1 << std::endl;
  return 0;
}
