Problem: A: Hokkaido University Easy

Note

Please note that the problem settings are the same as problem B, except for the constraints.

story

Homura-chan, who passed Hokkaido University and is excited about the beginning of a new life. But in front of her, a huge campus awaits ...

"Eh ... I'm not in time for the next class ..."

problem

Hokkaido University Sapporo Campus is famous for being unusually large. The Sapporo campus is represented by rectangular squares with H squares vertically and W squares horizontally. We will use (i, j) to represent the cells that are i-mass from the north and j-mass from the west. There are several buildings on campus, with a'B'if there is a building in the location represented by the square (i, j) and a'.' If not, in c_ {i, j}.

Homura, a freshman at Hokkaido University, was surprised at the size of the campus and was worried about moving between buildings. So I was wondering how far the farthest of the two squares with the building were. Here we define the distance between two pairs of squares (i, j), (i', j') as | i-i'| + | j-j'|.

Homura found this problem difficult for him and asked his classmates for help. Please ask for an answer instead of Homura-chan.

Input format


H W
c_ {11} c_ {12} ... c_ {1W}
::
c_ {H1} c_ {H2} ... c_ {HW}


Constraint

* 2 \ leq H, W \ leq 30
* H and W are integers
* c_ {i, j} is either'B'or'.'.
* At least two of c_ {i, j} are'B'.



Output format

Print the integer that represents the answer on one line.

Input example 1


3 3
B.B
..B
.BB


Output example 1


Four

* The longest is between the two points (1,1) and (3,3).



Input example 2


4 3
B ..
B ..
...
...


Output example 2


1

* Adjacent positions may be the longest.



Input example 3


6 6
... B ..
B.B.B.
.B.B.B
... B.B
.B..B.
..B ...


Output example 3


7





Example

Input

3 3
B.B
..B
.BB


Output

4

Solution (in CPP):

Solution:
#include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_set>
#include<unordered_map>
#include<fstream>
#include<list>
#include<functional>
#include<bitset>
#include<complex>
#include<tuple>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;

template<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}
template<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}
template<typename T,typename U>ostream & operator << (ostream &o,pair<T,U> &A){o<<A.F<<" "<<A.S; return o;}
template<typename T>ostream & operator << (ostream &o,vector<T> &A){ll i=A.size(); for(auto &I:A){o<<I<<(--i?" ":"");} return o;}
template<typename T>vector<T> & cset(vector<T> &A,T e=T()){for(auto &I:A){I=e;} return A;}


namespace Geometry{
    typedef long double D;
    typedef complex<long double> P;
    typedef pair<P,D> C;
    
    const D EPS=1e-9;
    const D PI=asin(1)*2;
    const D INF=1e18;
    
    static bool comp(const P &p1,const P &p2){
        if(p1.real()==p2.real()){return p1.imag()<p2.imag();}
        return p1.real()<p2.real();
    }
    
    static bool comp_y(const P &p1,const P &p2){
        if(p1.imag()==p2.imag()){return p1.real()<p2.real();}
        return p1.imag()<p2.imag();
    }
    
    static bool eqls(const P &p1,const P &p2){return abs(p1-p2)<EPS;}
    
    D dot(P p1,P p2){return p1.real()*p2.real()+p1.imag()*p2.imag();}
    
    D cross(P p1,P p2){return p1.real()*p2.imag()-p1.imag()*p2.real();}
    
    P project(P vec,P x){return vec*(x/vec).real();}
    
    P project(P p1,P p2,P x){return p1+project(p2-p1,x-p1);}
    
    P reflect(P vec,P x){return vec*conj(x/vec);}
    
    P reflect(P p1,P p2,P x){return p1+reflect(p2-p1,x-p1);}
    
    bool intersectSL(P p1,P p2,P vec){
        vec/=abs(vec); p1/=vec; p2/=vec;
        return (p1.imag()<EPS && p2.imag()>-EPS) || (p1.imag()>-EPS && p2.imag()<EPS);
    }
    
    bool intersectSL(P p1,P p2,P p3,P p4){return intersectSL(p1-p4,p2-p4,p3-p4);}
    
    bool intersectSS(P p1,P p2,P p3,P p4){
        if(dot(p2-p1,p3-p1)<-EPS && dot(p2-p1,p4-p1)<-EPS){return false;}
        if(dot(p1-p2,p3-p2)<-EPS && dot(p1-p2,p4-p2)<-EPS){return false;}
        return intersectSL(p1,p2,p3,p4) && intersectSL(p3,p4,p1,p2);
    }
    
    D distLP(P vec,P x){return abs((x/vec).imag())*abs(vec);}
    
    D distLP(P p1,P p2,P x){return distLP(p2-p1,x-p1);}
    
    D distSP(P p1,P p2,P x){
        if(dot(p2-p1,x-p1)<-EPS){return abs(x-p1);}
        if(dot(p1-p2,x-p2)<-EPS){return abs(x-p2);}
        return distLP(p1,p2,x);
    }
    
    D distSS(P p1,P p2,P p3,P p4){
        if(intersectSS(p1,p2,p3,p4)){return 0.0;}
        return min({distSP(p1,p2,p3),distSP(p1,p2,p4),
            distSP(p3,p4,p1),distSP(p3,p4,p2)});
    }
    
    P crosspointLL(P p1,P p2,P vec){
        if(abs(cross(p2-p1,vec))<EPS){return vec;}
        return vec*cross(p2-p1,p2)/cross(p2-p1,vec);
    }
    
    P crosspointLL(P p1,P p2,P p3,P p4){
        return p4+crosspointLL(p1-p4,p2-p4,p3-p4);
    }
    
    P crosspointSS(P p1,P p2,P p3,P p4){
        if(distSP(p1,p2,p3)<EPS){return p3;}
        if(distSP(p1,p2,p4)<EPS){return p4;}
        return crosspointLL(p1,p2,p3,p4);
    }
    
    bool intersectShL(P p1,P p2,P vec){
        vec/=abs(vec);
        return intersectSL(p1,p2,vec) &&
        crosspointLL(p1/vec,p2/vec,vec/vec).real()>-EPS;
    }
    
    bool intersectShL(P p1,P p2,P p3,P p4){
        return intersectShL(p1-p3,p2-p3,p4-p3);
    }
    
    //1::in,0::on edge,-1::out
    int contain(const vector<P> &poly,const P &p){
        vector<P> A={{65537,96847},{-24061,6701},{56369,-86509},{-93763,-78049},{56957,10007}};
        int s=A.size();
        vector<bool> cnt(s,false);
        for(int i=0;i<(int)poly.size();i++){
            int k=(i+1)%poly.size();
            if(distSP(poly[i],poly[k],p)<EPS){return 0;}
            for(int j=0;j<s;j++){
                if(intersectShL(poly[i],poly[k],p,p+A[j])){cnt[j]=!cnt[j];}
            }
        }
        int in=0;
        for(int j=0;j<s;j++){if(cnt[j]){in++;}}
        return in>s/2?1:-1;
    }
    
    vector<P> convexcut(const vector<P> &poly,P p1,P p2){
        vector<P> ret;
        for(int i=0;i<(int)poly.size();i++){
            int j=(i+1)%poly.size();
            if(cross(p2-p1,poly[i]-p1)>-EPS){ret.push_back(poly[i]);}
            if(intersectSL(poly[i],poly[j],p1,p2) &&
               distLP(p1,p2,poly[i])>EPS && distLP(p1,p2,poly[j])>EPS){
                ret.push_back(crosspointLL(poly[i],poly[j],p1,p2));
            }
        }
        return ret;
    }
    
    D area(const vector<P> &poly){
        D ans=0;
        for(int i=2;i<(int)poly.size();i++){
            ans+=cross(poly[i-1]-poly[0],poly[i]-poly[0]);
        }
        return abs(ans)/2;
    }
    
    vector<P> convexhull(vector<P> pts){
        vector<P> ret;
        sort(pts.begin(),pts.end(),comp);
        auto fnc=[&](){
            for(auto &I:pts){
                if(!ret.empty() && abs(I-ret.back())<EPS){continue;}
                while(ret.size()>1){
                    P A=ret.back()-ret[ret.size()-2];
                    P B=I-ret.back();
                    if(cross(A,B)<-EPS){ret.pop_back();}
                    else if(abs(cross(A,B))<EPS && dot(A,B)>EPS){ret.pop_back();}
                    else{break;}
                }
                ret.push_back(I);
            }
        };
        fnc();
        reverse(pts.begin(),pts.end());
        fnc();
        if(ret.size()>1 && ret[0]==ret.back()){ret.pop_back();}
        return ret;
    }
};

using namespace Geometry;


int main(){
    ll h,w;
    cin>>h>>w;
    vector<string> A(h);
    cin>>A;
    vector<P> pt;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(A[i][j]=='B'){pt.push_back(P(i,j));}
        }
    }
    pt=convexhull(pt);
    D ans=0;
    for(auto &I:pt){
        for(auto &J:pt){
            ans=max(ans,abs(I.real()-J.real())+abs(I.imag()-J.imag()));
        }
    }
    cout<<fixed<<setprecision(0)<<ans<<endl;
    
    
    return 0;
}