#include <bits/stdc++.h>

using namespace std;

int root(int i, int dsu[])
{
    if(i != dsu[i])
    {
        dsu[i] = root(dsu[i], dsu);
    }

    return dsu[i];
}

void connect(int a, int b, int dsu[])
{
    dsu[root(a, dsu)] = root(b,dsu);
}

bool connected(int a, int b, int dsu[])
{
    return root(a, dsu) == root(b, dsu);
}

bool moze(int k, char c[][500], int dsu[], int n,int Ai, int Aj, int Bi, int Bj)
{
    bool moze = false;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(connected(Ai*n+Aj, i*n+j, dsu))
                {
                    //dole desno
                    for(int j1 = j; j1 < min(n, k+j+1); j1++)
                    {
                        if(connected(max(0, i-1)*n+j1, Bi*n+Bj, dsu))
                            moze = true;
                    }

                    for(int i1 = i; i1 < min(n, k+i+1); i1++)
                    {
                        if(connected(i1*n+max(0, j-1), Bi*n+Bj, dsu))
                            moze = true;
                    }

                    for(int j1 = j; j1 < min(n, k+j+1); j1++)
                    {
                        if(connected(min(n-1, i+k+1)*n+j1, Bi*n+Bj, dsu))
                            moze = true;
                    }

                    for(int i1 = i; i1 < min(n, k+i+1); i1++)
                    {
                        if(connected(i1*n+min(n-1, j+k+1), Bi*n+Bj, dsu))
                            moze = true;
                    }


                    //gore levo
                    for(int j1 = j; j1 >= max(0, j-k); j1--)
                    {
                        if(connected(max(0, i-1)*n+j1, Bi*n+Bj, dsu))
                            moze = true;
                    }

                    for(int i1 = i; i1 >= max(0, i-k); i1--)
                    {
                        if(connected(i1*n+max(0, j-1), Bi*n+Bj, dsu))
                            moze = true;
                    }

                    for(int j1 = j; j1 >= max(0, j-k); j1--)
                    {
                        if(connected(min(n-1, i+k+1)*n+j1, Bi*n+Bj, dsu))
                            moze = true;
                    }

                    for(int i1 = i; i1 < max(0, i-k); i1--)
                    {
                        if(connected(i1*n+min(n-1, j+k+1), Bi*n+Bj, dsu))
                            moze = true;
                    }
                }
            }
        }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    char c[n][500];
    int Ai, Aj, Bi, Bj;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            cin >> c[i][j];
            if(c[i][j] == 'A')
            {
                Ai = i;
                Aj = j;
            }

            else if(c[i][j] == 'B')
            {
                Bi = i;
                Bj = j;
            }
        }
    }

    int dsu[n*n];
    for(int i = 0; i < n*n; i++)
        dsu[i] = i;

    c[Ai][Aj] = '.';
    c[Bi][Bj] = '.';

    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(c[i][j] == '.')
            {
                if(i-1 >= 0 && c[i-1][j] =='.')
                    connect((i-1)*n+j, i*n+j, dsu);
                if(j-1 >= 0 && c[i][j-1] == '.')
                    connect(i*n+j-1, i*n+j, dsu);
                if(i+1 < n && c[i+1][j] == '.')
                    connect((i+1)*n+j, i*n+j, dsu);
                if(j+1 < n && c[i][j+1] == '.')
                    connect(i*n+j+1, i*n+j, dsu);
            }
        }


    }

    int l = 0, r = n*n;
    while(r-l > 6)
    {
        int mid = (l+r)/2;
        if(moze(mid, c, dsu, n, Ai, Aj, Bi, Bj))
            r = mid;
        else
            l = mid;
    }

    for(int i = l; i <= r; i++)
    {
        if(moze(i, c, dsu, n, Ai, Aj, Bi, Bj))
        {
            cout << i;
            break;
        }
    }


}
/*
6 1
A.XX..
..XX..
XXXX..
..XX..
..XX..
..XX.B

6 1
A.XX..
..XX..
XXXXXX
..XXX.
..XX..
..XX.B

*/