#include <bits/stdc++.h>   using namespace std;   const int oo = ~0u >> 2, mo = (int)1e9 + 7;   const int mn = 52, mp = ((mn) * (mn)), me = mp * 4;   const int fx[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};   struct EDGE {     int y, l;   } E[me];   int g[mp], d[mp], h[mn][mn], Q[mp];   bool a[mn][mn];   int n, m, tt(0);   void add(int x, int y) { E[++tt].y = y, E[tt].l = g[x], g[x] = tt; }   void fill(int x, int y) {     h[x][y] = tt;     for (int i = 0; i <= 3; ++i) {       int nx = x + fx[i][0], ny = y + fx[i][1];       if (nx < 1 || nx > n || ny < 1 || ny > m || a[x][y] != a[nx][ny] ||           h[nx][ny])         continue;       fill(nx, ny);     }   }   int calc(int x, int y) {     int l = 0, r = 0;     memset(&d, -1, sizeof(d));     d[Q[++r] = h[x][y]] = 0;     while (l++ != r)       for (int i = g[Q[l]], y = E[i].y; i; i = E[i].l, y = E[i].y)         if (d[y] == -1) d[Q[++r] = y] = d[Q[l]] + 1;     int ret = 0;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (a[i][j]) (ret = max(ret, d[h[i][j]]));     return ret;   }   int main() {     scanf( %d%d , &n, &m);     bool black = 0;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) {         char ch;         do {           scanf( %c , &ch);         } while (ch !=  B  && ch !=  W );         black |= (a[i][j] = (ch ==  B ));       }     if (!black) {       printf( 0 n );       return 0;     }     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         if (!h[i][j]) ++tt, fill(i, j);     tt = 0;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j)         for (int k = 0; k <= 3; ++k) {           int x = i + fx[k][0], y = j + fx[k][1];           if (x < 1 || x > n || y < 1 || y > m || h[i][j] == h[x][y]) continue;           add(h[i][j], h[x][y]);         }     int Ans = oo;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= m; ++j) {         int tmp = calc(i, j);         (Ans = min(Ans, tmp));       }     printf( %d n , Ans + 1);   }   