#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pf push_front
int n,m,dp[2000][2000],x,y,x1,y1;
int lista[2000][2000],bio[2000][1000],veza[2000][2000];
string s;
queue<pair<int,int>> q;
vector<pair<int,int>> v;

/*
7 1
AXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXB

4 1
..XB
XXX.
XAX.
XXXX

*/
bool check(int mid)
{
    for(auto o:v){
        int x=o.first;
        int y=o.second;
        for(int i=max(1,y-mid-1);i<=min(n,y+mid+1);i++){
            for(int j=max(1,x-mid-1);j<=min(n,x+mid+1);j++){
                if(i==y+mid+1 && j==x+mid+1) continue;
                if(i==y-mid-1 && j==x-mid-1) continue;
                if(i==y-mid-1 && j==x+mid+1) continue;
                if(i==y+mid+1 && j==x-mid-1) continue;
                if(veza[i][j])return true;
            }
        }
    }
    return false;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;

    for(int i=1;i<=n;i++){
        cin >> s;
        for(int j=1;j<=n;j++){
            if(s[j-1]=='A'){
                x=j;
                y=i;
                lista[i][j]=0;
            }
            else if(s[j-1]=='B'){
                x1=j;
                y1=i;
                lista[i][j]=0;
            }
            else if(s[j-1]=='.')lista[i][j]=0;
            else lista[i][j]=1;
        }
    }

    q.push({x,y});
    bio[y][x]=1;
    while(!q.empty()){
        auto top = q.front();
        int j=top.first;
        int i = top.second;
        q.pop();
        //cout << i << ' ' << j << endl;
        if(lista[i][j+1]==1 || lista[i][j-1]==1 || lista[i+1][j] || lista[i-1][j] || lista[i-1][j+1] || lista[i+1][j-1] || lista[i+1][j+1] || lista[i-1][j-1])v.pb({j,i});
        if(lista[i][j+1]==0 && j+1<=n && bio[i][j+1]==0){
            q.push({j+1,i});
            bio[i][j+1]=1;
        }
        if(lista[i][j-1]==0 && j-1>0 && bio[i][j-1]==0){
            q.push({j-1,i});
            bio[i][j-1]=1;
        }
        if(lista[i+1][j]==0 && i+1<=n && bio[i+1][j]==0){
            q.push({j,i+1});
            bio[i+1][j]=1;
        }
        if(lista[i-1][j]==0 && i-1>0 && bio[i-1][j]==0){
            q.push({j,i-1});
            bio[i-1][j]=1;
        }
    }
    if(bio[y1][x1]){
        cout << -1;
        return 0;
    }

    q.push({x1,y1});
    veza[y1][x1]=1;
    while(!q.empty()){
        auto top = q.front();
        int j=top.first;
        int i = top.second;
        q.pop();
        if(lista[i][j-1]==0 && j-1>0 && veza[i][j-1]==0){
            q.push({j-1,i});
            veza[i][j-1]=1;
        }
        if(lista[i+1][j]==0 && i+1<=n && veza[i+1][j]==0){
            q.push({j,i+1});
            veza[i+1][j]=1;
        }
        if(lista[i-1][j]==0 && i-1>0 && veza[i-1][j]==0){
            q.push({j,i-1});
            veza[i-1][j]=1;
        }
        if(lista[i][j+1]==0 && j+1<=n && veza[i][j+1]==0){
            q.push({j+1,i});
            veza[i][j+1]=1;
        }
    }
/*
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout << veza[i][j]<< ' ';
        }
        cout << endl;
    }
*/
    int l=0,r=n+2;

    for(int i=0;i<=n+5;i++){
        check(i);
        cout << i;
    }

    /*
    while(l<r-1){
        int mid=(l+r)/2;
        if(check(mid))r=mid;
        else l=mid;
    }

    for(int i=r-2;i<=l+2;i++){
        if(i<=0)continue;
        if(check(i)){
            cout << i;
            return 0;
        }
    }
*/
    return 0;
}