#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
int n, m;
string temp[MAXN];
bool g[MAXN][MAXN];
int get_num(int x, int y) { return m * (x - 1) + y; }
int head[MAXN * MAXN];
struct node {
  int from;
  int to;
  int val;
  int next;
} edge[MAXN * MAXN << 2];
int edge_cnt;
int sum;
void add_edge(int fr, int to, int val) {
  edge[++edge_cnt].from = fr;
  edge[edge_cnt].to = to;
  edge[edge_cnt].val = val;
  edge[edge_cnt].next = head[fr];
  head[fr] = edge_cnt;
  sum += val;
}
bool vis[MAXN * MAXN];
int dis[MAXN * MAXN];
priority_queue<pair<int, int> > q;
void dij(int s) {
  memset(vis, 0, sizeof(vis));
  memset(dis, 0x3f, sizeof(dis));
  dis[s] = 0;
  q.push(make_pair(0, s));
  while (q.size()) {
    int x = q.top().second;
    q.pop();
    if (vis[x]) continue;
    vis[x] = 1;
    for (int i = head[x]; i; i = edge[i].next) {
      int y = edge[i].to, z = edge[i].val;
      if (dis[y] > dis[x] + z) {
        dis[y] = dis[x] + z;
        q.push(make_pair(-dis[y], y));
      }
    }
  }
}
vector<int> black;
int ans = 0x7fffffff;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> temp[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < m; j++) {
      if (temp[i][j] == 'B') {
        g[i][j + 1] = 1;
        black.push_back(get_num(i, j + 1));
      }
    }
  }
  if (!black.size()) {
    cout << "0" << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (i - 1 >= 1)
        add_edge(get_num(i, j), get_num(i - 1, j), g[i][j] != g[i - 1][j]);
      if (j - 1 >= 1)
        add_edge(get_num(i, j), get_num(i, j - 1), g[i][j] != g[i][j - 1]);
      if (i + 1 <= n)
        add_edge(get_num(i, j), get_num(i + 1, j), g[i][j] != g[i + 1][j]);
      if (j + 1 <= m)
        add_edge(get_num(i, j), get_num(i, j + 1), g[i][j] != g[i][j + 1]);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dij(get_num(i, j));
      int temp = 0;
      for (int o = 0; o < black.size(); o++) temp = max(temp, dis[black[o]]);
      ans = min(ans, temp);
    }
  }
  ans++;
  cout << ans << endl;
  return 0;
}
