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;

    auto L = new int[](H);
    auto R = new int[](H);
    L[] = 1 << 29;
    R[] = -1;

    foreach (i; 0..H) foreach (j; 0..W) if (A[i][j] == 'B') {
        L[i] = min(L[i], j);
        R[i] = max(R[i], j);
    }

    int ans = 0;

    foreach (i; 0..H) foreach (j; i+1..H) if (R[i] != -1 && R[j] != -1) {
        ans = max(ans, abs(i - j) + abs(R[i] - L[j]));
        ans = max(ans, abs(i - j) + abs(L[i] - R[j]));
    }

    foreach (i; 0..H) if (R[i] != -1) {
        ans = max(ans, R[i] - L[i]);
    }

    ans.writeln;
}
