#include <cstdio>
#include <algorithm>
using namespace std;
int x[901];
int y[901];
char row[31];

int main()
{
    int H, W, idx = 0, maxd = 0;
    scanf("%d %d", &H, &W);
    for (int i = 0; i < H; i++) {
        scanf("%s", row);
        for (int j = 0; j < W; j++) if (row[j] == 'B') { x[idx] = i; y[idx] = j; idx++; }
    }
    for (int i = 0; i < idx-1; i++) {
        for (int j = i + 1; j < idx; j++) {
            maxd = max(maxd, abs(x[j]-x[i]) + abs(y[j]-y[i]));
        }
    }
    printf("%d\n", maxd);
    return 0;
}
