#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
int n, m, velicina;
char a[1000][1000];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int ai, aj, bi, bj;
struct leb{
    int x, y;
};
leb los = {-3, -3};
vector<leb>v;
int clamp(int a, int l, int r){
    if(a > r)
        a = r;
    if(a < l)
        a = l;
    return a;
}
int desno(int i, int j){

    for(int p = 0; p < v.size(); p++)
        if(v[p].y == j - velicina)
            v[p] = los;
    for(int p = i - velicina; p <= i + velicina; p++)
        if(a[clamp(p, 1, n)][clamp(j + velicina + 1, 1, n)] == '2'){
            leb pom;
            pom.x = p;
            pom.y = j + velicina + 1;
            v.push_back(pom);
            ///provera
        }


}
int levo(int i, int j){

    for(int p = 0; p < v.size(); p++)
        if(v[p].y == j + velicina)
            v[p] = los;
    for(int p = i - velicina; p <= i + velicina; p++)
        if(a[clamp(p, 1, n)][clamp(j - velicina - 1, 1, n)] == '2'){
            leb pom;
            pom.x = p;
            pom.y = j - velicina - 1;
            v.push_back(pom);
            ///provera
        }


}
int dole(int i, int j){

    for(int p = 0; p < v.size(); p++)
        if(v[p].x == i - velicina)
            v[p] = los;
    for(int p = j - velicina; p <= j + velicina; p++)
        if(a[clamp(i + velicina + 1, 1, n)][clamp(p, 1, n)] == '2'){
            leb pom;
            pom.x = i + velicina + 1;
            pom.y = p;
            v.push_back(pom);
            ///provera
        }



}
int gore(int i, int j){

    for(int p = 0; p < v.size(); p++)
        if(v[p].x == i + velicina)
            v[p] = los;
    for(int p = j - velicina; p <= j + velicina; p++)
        if(a[clamp(i - velicina - 1, 1, n)][clamp(p, 1, n)] == '2'){
            leb pom;
            pom.x = i - velicina - 1;
            pom.y = p;
            v.push_back(pom);
            ///provera
        }

}
int dfs(int x, int y, char filer){

    a[x][y] = filer;

    for(int i = 0; i < 4; i++)
        if(a[x + dx[i]][y + dy[i]] == '.')
            dfs(x + dx[i], y + dy[i], filer);

}
int provera(int i, int j){
    if(a[i][j] == '1')
        for(int pom = 0; pom < v.size(); pom++)
            if(v[pom].x != los.x)
                return 1;
    return 0;
}
void ispis(){
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++)
            cout << a[i][j];
        cout << endl;
    }
}
int main(){

    freopen("ulaz.txt", "r", stdin);
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            cin >> a[i][j];

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++){
            if(a[i][j] == 'A'){
                ai = i;
                aj = j;
            }
            if(a[i][j] == 'B'){
                bi = i;
                bj = j;
            }
        }
    dfs(ai, aj, '1');
    ispis();
    cout << endl;
    dfs(bi, bj, '2');
    ispis();

    velicina = 0;
    while(1){
        v.clear();
        int x = 1;
        int y = 1;
        for(int i = 1; i <= velicina * 2 + 1; i++)
            for(int j = 1; j <= velicina * 2 + 1; j++)
                if(a[i][j] == '2'){
                    leb pom;
                    pom.x = i;
                    pom.y = j;
                }
        if(provera(x, y)){
            cout << velicina << endl;
            return 0;
        }
        int k = 1;
        cout << endl << endl << "V = " << velicina << endl;
        while(1){
            if(y > n or y < 1){
                k = -k;
                if(x + 1 == n){
                    break;
                    cout << "iskok\n";
                }
                x++;
                dole(x, y);
                if(provera(x, y)){
                    cout << velicina << endl;
                    return 0;
                }
            }
            cout << "velicina = " << velicina << " x = " << x << " y = " << y << endl;
            if(k == 1)
                desno(x, y);
            else if(k == -1)
                levo(x, y);
            y += k;
            if(provera(x, y)){
                cout << velicina << endl;
                return 0;
            }
        }
        velicina++;
    }




    return 0;
}