#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;

string canpas[31];

int main() {
    int H, W;
    cin >> H >> W;
    rep(i, H) cin >> canpas[i];

    vector<P> pos;
    rep(r, H) rep(c, W) {
        if (canpas[r][c] == 'B') pos.emplace_back(r, c);
    }

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

    cout << ans << endl;

    return 0;
}

