    #include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
typedef long long ll;

int main() {
    int h, w;
    cin >> h >> w;

    char s[1010][1010];
    vector<pair<int, int> > b;
    for (int i = 0; i < h; i++) {
        cin >> s[i];
        for (int j = 0; j < w; j++) {
            if (s[i][j] == 'B') b.emplace_back(i, j);
        }
    }

    int ans = 0;
    for (int i = 0; i < b.size(); i++) {
        for (int j = 0; j < b.size(); j++) {
            ans = max(ans, abs(b[i].first - b[j].first)
            + abs(b[i].second - b[j].second));
        }
    }

    //cout << far_i << " " << far_j << endl;

    cout << ans << endl;

}
