#include<bits/stdc++.h>
using namespace std;
int N,m,mu,bi,bj;
struct slog{
    int i;
    int j;
} p;
vector<slog> A;
char polje[505][505];
bool markirali[505][505];
bool mog;
bool moguce(int i,int j,int ci, int cj){
    //cout<<i<<" "<<j<<endl;
    if(i>N or j>N or i<1 or j<1 or polje[i][j]=='X' or markirali[i][j])
        return false;
    if(i==ci and j==cj)
        return true;
    markirali[i][j]=1;
    return moguce(i+1,j,ci,cj) or moguce(i-1,j,ci,cj) or moguce(i,j+1,ci,cj) or moguce(i,j-1,ci,cj);
}
bool unisti(int i, int j, int x) {
    for(int d=max(i-x,1);d<=min(N,i+x);d++)
        for(int k=max(j+x,1);j<=min(N,j-x);k++)
            if(polje[d][k]=='X')
                polje[d][k]='O';
    for(int d=1;d<=N;d++)
        for(int k=1;k<=d;k++)
            markirali[d][k]=0;
    mog=moguce(A[0].i,A[0].j,bi,bj);
    for(int d=max(i-x,1);d<=min(N,i+x);d++)
        for(int k=max(j+x,1);k<=min(N,j-x);k++)
            if(polje[d][k]=='O')
                polje[d][k]='X';
    return mog;
}
void markiraj(int i, int j, char m, int pi, int pj) {
    if(i>N or j>N or i<1 or j<1 or polje[i][j]=='X' or markirali[i][j])
        return;
    if((polje[i][j]=='A' and m=='B')or(polje[i][j]=='B' and m=='A')) {
        cout<<0<<endl;
        exit(0);
    }
    //cout<<i<<" "<<j<<endl;
    polje[i][j]=m;
    markirali[i][j]=1;
    markiraj(i+1,j,m,i,j);
    markiraj(i-1,j,m,i,j);
    markiraj(i,j+1,m,i,j);
    markiraj(i,j-1,m,i,j);
    return;
}
bool moguc(int x) {
    for(int i=0;i<A.size();i++)
        if(unisti(A[i].i,A[i].j,x))
            return true;
    return false;

}
bool debug=false;
int main() {
    ios_base::sync_with_stdio(false);
    cin>>N>>m;
    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++) {
            cin>>polje[i][j];
            if(polje[i][j]=='B') {
                bi=i;
                bj=j;
            }
        }
    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)
            if(polje[i][j]=='A') {
                for(int d=1;d<=N;d++)
                    for(int k=1;k<=d;k++)
                        markirali[d][k]=0;
                markiraj(i,j,'A',i,j);
                break;
            }
    if(debug) cout<<"markirano"<<endl;
    for(int d=1;d<=N;d++)
        for(int k=1;k<=d;k++)
            markirali[d][k]=0;
    for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)
            if(polje[i][j]=='X') {
                if(polje[i+1][j]=='A' and !markirali[i+1][j]) {
                    p.i=i+1;
                    p.j=j;
                    markirali[p.i][p.j]=1;
                    A.push_back(p);
                }
                if(polje[i-1][j]=='A' and !markirali[i-1][j]) {
                    p.i=i-1;
                    p.j=j;
                    markirali[p.i][p.j]=1;
                    A.push_back(p);
                }
                if(polje[i][j+1]=='A' and !markirali[i][j+1]) {
                    p.i=i;
                    p.j=j+1;
                    markirali[p.i][p.j]=1;
                    A.push_back(p);
                }
                if(polje[i][j-1]=='A' and !markirali[i][j-1]) {
                    p.i=i;
                    p.j=j-1;
                    markirali[p.i][p.j]=1;
                    A.push_back(p);
                }
            }
    if(debug) cout<<"bp"<<endl;
    int dg=0,gg=500,sred;
    while(gg-1>dg) {
       // cout<<dg<<" "<<gg<<endl;
        sred=(dg+gg)/2;
        if(moguc(sred))
            gg=sred;
        else
            dg=sred;
    }
  //  for(int i=0;i<A.size();i++)
   //    cout<<A[i].i<<" "<<A[i].j<<endl;
    cout<<dg<<endl;
    return 0;
}