#include<cstdio>
#include<iostream>
using namespace std;
int sum=0,n,ff=0,x[5]={0,1,0,0,-1},y[5]={0,0,1,-1,0},f[501][501],xx[501],yy[501];
int ans[501][501];
char map[501][501];
int main()
{
int sbx,sby;
scanf("%d",&n);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
char r;
f[i][j]=1;
cin>>r;
map[i][j]=r;
if (r=='A') {
xx[1]=i;
yy[1]=j;
f[i][j]=0;
}
if (r=='B'){
sbx=i;
sby=j;
}
if (r=='2'||r=='4'||r=='0') f[i][j]=0;
}
int head=0,tail=1;
while (head!=tail)
{
head++;
int aa=xx[head],bb=yy[head];
for (int i=1;i<=4;i++)
{
int a=xx[head]+x[i],b=yy[head]+y[i];
if (f[a][b]&&a<=n&&a>0&&b<=n&&b>0)
{
f[a][b]=0;
xx[++tail]=a;
yy[tail]=b;
ans[a][b]=ans[aa][bb]+1;
if (a==sbx&&b==sby)
{
ff=1;
break;
}
}
}
}
if (ff) cout<<ans[sbx][sby]*2;
else cout<<"NoNE!";
}