#include <bits/stdc++.h>
using namespace std;
int movi[] = {1, -1, 0, 0};
int movj[] = {0, 0, 1, -1};
int costeexpansion(vector<vector<int> > v, int i, int j) {
  int passos = 0;
  int val = v[i][j];
  v[i][j] = -1;
  stack<pair<int, int> > s;
  s.push(pair<int, int>(i, j));
  while (not s.empty()) {
    val = 1 - val;
    passos++;
    stack<pair<int, int> > nexts;
    while (not s.empty()) {
      pair<int, int> p = s.top();
      s.pop();
      i = p.first;
      j = p.second;
      for (int k = 0; k < 4; k++) {
        int nexti = i + movi[k];
        int nextj = j + movj[k];
        if (v[nexti][nextj] == 1 - val) {
          v[nexti][nextj] = -1;
          s.push(pair<int, int>(nexti, nextj));
        } else if (v[nexti][nextj] != -1) {
          v[nexti][nextj] = -1;
          nexts.push(pair<int, int>(nexti, nextj));
        }
      }
    }
    s = nexts;
  }
  passos -= val;
  return passos;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<int> > v(n + 2, vector<int>(m + 2, -1));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      int val;
      if (c == 'B')
        val = 1;
      else
        val = 0;
      v[i + 1][j + 1] = val;
    }
  }
  int coste = 1000000000;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      coste = min(coste, costeexpansion(v, i + 1, j + 1));
  cout << coste << endl;
}
