#include <bits/stdc++.h>
using namespace std;
struct edge {
  int v, w;
  edge() {}
  edge(int _v, int _w) : v(_v), w(_w) {}
};
vector<edge> E[2510];
int n, m, a[50][50];
int Q[50000], l, r, d[2510], inq[2510], ans = 19941;
void Work() {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      for (int s = 0; s < n; s++)
        for (int t = 0; t < m; t++) {
          d[((s)*m + (t))] = -1;
          inq[((s)*m + (t))] = 0;
        }
      d[((i)*m + (j))] = 0;
      inq[((i)*m + (j))] = 1;
      l = 0;
      r = 1;
      Q[0] = ((i)*m + (j));
      while (l < r) {
        int u = Q[l++];
        for (int p = 0; p < E[u].size(); p++) {
          int v = E[u][p].v, w = E[u][p].w;
          if (d[v] == -1 || d[v] > d[u] + w) {
            d[v] = d[u] + w;
            if (!inq[v]) inq[Q[r++] = v] = 1;
          }
        }
        inq[u] = 0;
      }
      int maxd = -1;
      for (int s = 0; s < n; s++)
        for (int t = 0; t < m; t++)
          if (a[s][t]) maxd = max(maxd, d[((s)*m + (t))]);
      ans = min(maxd + 1, ans);
    }
  if (ans == 19941) ans = 1;
  cout << ans;
}
void Init() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("\n");
    for (int j = 0; j < m; j++) {
      char ch;
      scanf("%c", &ch);
      a[i][j] = (ch == 'B');
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m - 1; j++) {
      E[((i)*m + (j))].push_back(
          edge(((i)*m + (j + 1)), (a[i][j] != a[i][j + 1])));
      E[((i)*m + (j + 1))].push_back(
          edge(((i)*m + (j)), (a[i][j] != a[i][j + 1])));
    }
  }
  for (int j = 0; j < m; j++) {
    for (int i = 0; i < n - 1; i++) {
      E[((i)*m + (j))].push_back(
          edge(((i + 1) * m + (j)), (a[i][j] != a[i + 1][j])));
      E[((i + 1) * m + (j))].push_back(
          edge(((i)*m + (j)), (a[i][j] != a[i + 1][j])));
    }
  }
}
int main() {
  Init();
  Work();
}
