import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

void main() {
    auto s = readln.split.map!(to!int);
    auto H = s[0];
    auto W = s[1];
    auto A = iota(H).map!(_ => readln.chomp).array;

    Tuple!(int, int)[] X;

    foreach (i; 0..H) foreach (j; 0..W) if (A[i][j] == 'B') {
        X ~= tuple(i, j);
    }

    int ans  = 0;
    foreach (i; 0..X.length.to!int) foreach (j; i+1..X.length.to!int) {
        ans = max(ans, abs(X[i][0] - X[j][0]) + abs(X[i][1] - X[j][1]));
    }

    ans.writeln;
}
