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

/*
7 1
AXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXB

*/
bool check(ll mid)
{
    for(auto i:v){
        ll x=i.first;
        ll y=i.second;
        for(int i=min(1LL,y-mid-1);i<=min(n,y+mid+1);i++){
            for(int j=min(1LL,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;
        }
    }
/*
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout << lista[i][j] << ' ';
        }
        cout << endl;
    }
    */
    q.push({x,y});
    bio[y][x]++;
    bool moze=false;
    while(!q.empty()){
        auto top = q.front();
        if(top.first == x1 && top.second==y1){
            moze=true;
            break;
        }
        ll j=top.first;
        ll 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(moze){
        cout << -1;
        return 0;
    }

    q.push({x1,y1});
    veza[y1][x1]=1;
    while(!q.empty()){
        auto top = q.front();
        ll j=top.first;
        ll 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;
        }
    }
/*
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cout << veza[i][j]<< ' ';
        }
        cout << endl;
    }
*/
    ll l=0,r=n+2;
    while(l<r-1){
        ll mid=(l+r)/2;
        if(check(mid))r=mid;
        else l=mid;
    }

    cout << r;
    return 0;
}