import java.util.*;
class Main{
static int n;
static int m;
static int dx[]= {0,0,-1,1};
static int dy[]= {1,-1,0,0};
static class node{
int x;
int y;
int cnt;
char p;
public node(int x, int y, int cnt,char p) {
this.x = x;
this.y = y;
this.cnt = cnt;
this.p=p;
}
}
static char a[][];
static int vis[][];
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
n=in.nextInt();
a=new char [n][n];
vis=new int [n][n];
int ax=0;
int ay=0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
String s=in.next();
a[i][j]=s.charAt(0);
if(a[i][j]=='A') {
ax=i;
ay=j;
}
if(a[i][j]=='B') {
bx=i;
by=j;
}
}
}
bfs(ax,ay);
if(flag) {
System.out.println(-1);
}
}
static int bx;
static int by;
static boolean flag=true;
static void bfs(int ax,int ay) {
Queue<node> q=new ArrayDeque<Main.node>();
q.add(new node(ax,ay,0,'A'));
vis[ax][ay]=1;
while(!q.isEmpty()) {
node u=q.peek();
q.poll();
vis[u.x][u.y]=1;
if(u.x==bx&&u.y==by) {
System.out.println(u.cnt);
flag=false;
break;
}
for(int i=0;i<dx.length;i++) {
int x=u.x+dx[i];
int y=u.y+dy[i];
if(x>=0&&y>=0&&x<n&&y<n&&vis[x][y]==0&&a[u.x][u.y]!=a[x][y]) {
vis[x][y]=1;
q.add(new node(x,y,u.cnt+1,a[x][y]));
}
}
}
}
}