#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
const int N = 505;
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};

int n,m,xa,xb,ya,yb;
bool canA[N][N],canB[N][N];
//int sum[N][N];
string s[N];

bool ok(int x,int y){
    if(x<1||x>n||y<1||y>n||s[x][y]!='.')return false;
    return true;
}

bool ok2(int x,int y){
    if(x<1||x>n||y<1||y>n)return false;
    return true;
}

/*int getsum(int x1,int y1,int x2,int y2){
    if(x1<1)x1=1;
    if(y1<1)y1=1;
    if(x2<1)x2=1;
    if(y2<1)y2=1;

    if(x1>n)x1=n;
    if(y1>n)y1=n;
    if(x2>n)x2=n;
    if(y2>n)y2=n;

    int r=sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1];
    r+=sum[x1-1][y2]-sum[x1-1][y1-1];
    r+=sum[x2+1][y2]-sum[x2+1][y2-1];
    r+=sum[x2][y1-1]-sum[x1-1][y1-1];
    r+=sum[x2][y2+1]-sum[x1-1][y2+1];


    return r;
}

bool solve(int k){
    if(m==1){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(!canA[i][j])continue;
                int s=getsum(i-k,j-k,i+k,j+k);
                if(s>0)return true;
            }
        }
        return false;
    }
     for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(!canA[i][j])continue;
            for(int t=j-k;t<=j+k;t++){
                if(ok2(i-k,t)){
                    int posx=i-k;
                    int posy=t;
                    int s=getsum(posx-k,posy-k,posx+k,posy+k);

                    if(s>0)return true;
                }

            }
            for(int t=j-k;t<=j+k;t++){
                if(ok2(i+k,t)){
                    int posx=i+k;
                    int posy=t;
                    int s=getsum(posx-k,posy-k,posx+k,posy+k);
                    if(s>0)return true;
                }
            }
            for(int t=i-k;t<=i+k;t++){
                if(ok2(t,j-k)){
                    int posx=t;
                    int posy=j-k;
                    int s=getsum(posx-k,posy-k,posx+k,posy+k);
                    if(s>0)return true;
                }

            }
            for(int t=i-k;t<=i+k;t++){
                if(ok2(t,j+k)){
                    int posx=t;
                    int posy=j+k;
                    int s=getsum(posx-k,posy-k,posx+k,posy+k);
                    if(s>0)return true;
                }
            }
        }
    }
    return false;
}*/


int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>s[i];
        s[i]='#'+s[i];
        for(int j=1;j<=n;j++){
            if(s[i][j]=='A'){
                xa=i;
                ya=j;
            }else if(s[i][j]=='B'){
                xb=i;
                yb=j;
            }
        }
    }

    queue<pair<int,int> >q;
    q.push({xa,ya});
    while(!q.empty()){
        auto e=q.front();
        canA[e.first][e.second]=true;
        q.pop();
        for(int i=0;i<4;i++){
            if(ok(e.first+dx[i],e.second+dy[i])&&!canA[e.first+dx[i]][e.second+dy[i]]){
                q.push({e.first+dx[i],e.second+dy[i]});
            }
        }
    }

    q.push({xb,yb});
    while(!q.empty()){
        auto e=q.front();
        canB[e.first][e.second]=true;
        q.pop();
        for(int i=0;i<4;i++){
            if(ok(e.first+dx[i],e.second+dy[i])&&!canB[e.first+dx[i]][e.second+dy[i]]){
                q.push({e.first+dx[i],e.second+dy[i]});
            }
        }
    }

    if(canA[xb][yb]){
        cout<<-1;
        return 0;
    }

    /*for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+canB[i][j];
        }
    }



    int lo=1;
    int hi=n;
    int ans=n;

    while(lo<=hi){
        int mid=(lo+hi)>>1;
        if(solve(mid)){
            ans=mid;
            hi=mid-1;
        }else{
            lo=mid+1;
        }
    }

    cout<<ans;*/
    return 0;
}