#include <bits/stdc++.h>
using namespace std;
char s[52][52];
int G[52][52];
int n, m;
queue<pair<int, int> > Q;
void flood(pair<int, int> x) {
  int u = x.first, v = x.second, c = G[u][v];
  if (u + 1 <= n && G[u + 1][v] == -1)
    if (s[u + 1][v] == s[u][v])
      G[u + 1][v] = c, flood(make_pair(u + 1, v));
    else
      Q.push(make_pair(u + 1, v)), G[u + 1][v] = c + 1;
  if (u - 1 > 0 && G[u - 1][v] == -1)
    if (s[u - 1][v] == s[u][v])
      G[u - 1][v] = c, flood(make_pair(u - 1, v));
    else
      Q.push(make_pair(u - 1, v)), G[u - 1][v] = c + 1;
  if (v + 1 <= m && G[u][v + 1] == -1)
    if (s[u][v + 1] == s[u][v])
      G[u][v + 1] = c, flood(make_pair(u, v + 1));
    else
      Q.push(make_pair(u, v + 1)), G[u][v + 1] = c + 1;
  if (v - 1 > 0 && G[u][v - 1] == -1)
    if (s[u][v - 1] == s[u][v])
      G[u][v - 1] = c, flood(make_pair(u, v - 1));
    else
      Q.push(make_pair(u, v - 1)), G[u][v - 1] = c + 1;
}
int main() {
  scanf("%d%d", &n, &m);
  int Res = n * m;
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  bool f = false;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i][j] == 'B') f = true;
  if (!f) {
    puts("0");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      memset(G, -1, sizeof G);
      Q.push(make_pair(i, j));
      G[i][j] = 0;
      while (!Q.empty()) {
        pair<int, int> u = Q.front();
        Q.pop();
        flood(u);
      }
      int res = 0;
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
          if (s[i][j] == 'B') res = max(res, G[i][j]);
      Res = min(Res, res + 1);
    }
  printf("%d\n", Res);
  return 0;
}
