#include <bits/stdc++.h>
using namespace std;
vector<int> g[5000];
int n, m;
int c[100][100];
char t[100][100];
bool used[100][100];
char clr[5000];
bool valid(int i, int j) { return (0 <= i && i < n && 0 <= j && j < m); };
int di[4] = {-1, 1, 0, 0};
int dj[4] = {0, 0, -1, 1};
void dfs(int i, int j, int color) {
  used[i][j] = true;
  c[i][j] = color;
  for (int k = 0; k < 4; k++) {
    int i1 = i + di[k];
    int j1 = j + dj[k];
    if (valid(i1, j1) && t[i1][j1] == t[i][j] && !used[i1][j1])
      dfs(i1, j1, color);
  }
}
void add_edge(int i, int j) {
  g[i].push_back(j);
  g[j].push_back(i);
}
void dfs1(int i, int j) {
  used[i][j] = true;
  for (int k = 0; k < 4; k++) {
    int i1 = i + di[k];
    int j1 = j + dj[k];
    if (valid(i1, j1)) {
      if (t[i1][j1] == t[i][j] && !used[i1][j1]) dfs1(i1, j1);
      if (t[i1][j1] != t[i][j]) add_edge(c[i1][j1], c[i][j]);
    }
  }
}
int l = 0;
bool _used[10000];
int h[3000];
int q[10000];
int bfs(int v) {
  for (int i = 0; i < l; i++) {
    _used[i] = false;
    h[i] = 1000 * 1000 * 1000;
  };
  int rp = 0;
  int wp = 1;
  q[0] = v;
  h[v] = 0;
  while (rp != wp) {
    v = q[rp];
    rp++;
    for (int i = 0; i < g[v].size(); i++) {
      int u = g[v][i];
      if (h[u] > h[v] + 1) {
        h[u] = h[v] + 1;
        q[wp] = u;
        wp++;
      }
    }
  }
  int vmax = 0;
  for (int i = 0; i < l; i++) vmax = max(vmax, h[i]);
  return vmax;
}
int main() {
  scanf("%d%d\n", &n, &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) scanf("%c", &t[i][j]);
    scanf("\n");
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (!used[i][j]) {
        clr[l] = t[i][j];
        dfs(i, j, l++);
      }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) used[i][j] = false;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (!used[i][j]) dfs1(i, j);
  int best = 1000 * 1000 * 1000;
  for (int i = 0; i < l; i++) {
    int cur = bfs(i);
    if (clr[i] == 'W' && (cur & 1)) cur++;
    if (clr[i] == 'B' && (cur % 2 == 0)) cur++;
    best = min(best, cur);
  }
  cout << best << endl;
  return 0;
}
