// Undone
#include <bits/stdc++.h>
using namespace std;
int H, W; 
char grid[2000][2000];
int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> grid[i][j];
    }
  }

  int ans = 0, found = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (grid[i][j] != 'B') continue;
      for (int k = H-1; k >= 0; k--) {
        for (int l = W-1; l >= 0; l--) {
          if (grid[k][l] != 'B') continue;
          ans = max(ans, abs(i-k)+abs(j-l));
        }
      }
    }
  }
  cout << ans << endl;
}


