#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
queue<P> q;
int n,d[105][105],l,cx,cy,step[105][105];
char a[105][105];
void bfs()
{
q.push(P(cx,cy));
d[cx][cy]=1;
while(!q.empty())
{
int x=q.front().first;
int y=q.front().second;
q.pop();
if(a[x][y]=='B')
{
cout<<step[x][y]-1;
l=1;
break;
}
int i=1;
while(1)
{
if(a[x+i][y]=='x'||d[x+i][y]==1||x+i>n)
{
break;
}
q.push(P(x+i,y));
d[x+i][y]=1;
step[x+i][y]=step[x][y]+1;
i++;
}
i=1;
while(1)
{
if(a[x-i][y]=='x'||d[x-i][y]==1||x-i<1)
{
break;
}
q.push(P(x-i,y));
d[x-i][y]=1;
step[x-i][y]=step[x][y]+1;
i++;
}
i=1;
while(1)
{
if(a[x][y+i]=='x'||d[x][y+i]==1||y+i>n)
{
break;
}
q.push(P(x,y+i));
d[x][y+i]=1;
step[x][y+i]=step[x][y]+1;
i++;
}
i=1;
while(1)
{
if(a[x][y-i]=='x'||d[x][y-i]==1||y-i<1)
{
break;
}
q.push(P(x,y-i));
d[x][y-i]=1;
step[x][y-i]=step[x][y]+1;
i++;
}
}
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j]=='A')
{
cx=i;
cy=j;
}
}
}
bfs();
if(l==0)
{
cout<<-1;
}
}