#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[51][51];
const int mx[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct Edge {
  int v, w, next;
} edge[25001];
int head[2501], cnt;
int vis[2501], dis[2501];
void add_edge(int u, int v, int w) {
  edge[++cnt].v = v;
  edge[cnt].w = w;
  edge[cnt].next = head[u];
  head[u] = cnt;
}
void spfa(int id) {
  deque<int> q;
  for (int i = 1; i <= m * n; i++) {
    vis[i] = 0;
  }
  vis[id] = 1;
  dis[id] = 0;
  q.push_back(id);
  while (!q.empty()) {
    int x = q.front();
    q.pop_front();
    for (int i = head[x]; i; i = edge[i].next) {
      if (!vis[edge[i].v]) {
        dis[edge[i].v] = dis[x] + edge[i].w;
        vis[edge[i].v] = 1;
        if (edge[i].w)
          q.push_back(edge[i].v);
        else
          q.push_front(edge[i].v);
      }
    }
  }
}
int main() {
  cin >> n >> m;
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      char temp;
      cin >> temp;
      a[i][j] = temp == 'W';
      if (temp == 'B') ans = 0x3f3f3f3f;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < 4; k++) {
        int nx = i + mx[k][0], ny = j + mx[k][1];
        if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) {
          add_edge((nx - 1) * m + ny, (i - 1) * m + j, a[i][j] != a[nx][ny]);
        }
      }
    }
  }
  for (int i = 1; i <= n * m; i++) {
    int Max = -1;
    spfa(i);
    for (int x = 1; x <= n; x++) {
      for (int y = 1; y <= m; y++) {
        if (a[x][y] == 0) Max = max(Max, dis[(x - 1) * m + y]);
      }
    }
    if (i == 1)
      ans = Max;
    else
      ans = min(Max, ans);
  }
  cout << ans + 1;
}
