#include <iostream>
#include <cstring>
using namespace std;
int n;
int f[105][105], vis[105][105];
char s[105][105];
int si, sj, ei, ej;
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
void dfs(int x, int y)
{
if (x == ei && x == ej)
return;
for (int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if (vis[xx][yy] == 0 && (xx >= 1 && xx <= n && yy >= 1 && yy <= n && s[x][y] != s[xx][yy]))
{
vis[xx][yy] = 1;
f[xx][yy] = min(f[xx][yy], f[x][y] + 1);
dfs(xx, yy);
vis[xx][yy] = 0;
}
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
cin >> s[i][j];
if (s[i][j] == 'A')
si = i, sj = j;
if (s[i][j] == 'B')
ei = i, ej = j;
}
memset(f, 0x3f, sizeof(f));
vis[si][sj] = 1;
f[si][sj] = 0;
dfs(si, sj);
if (f[ei][ej] == 0x3f3f3f3f)
cout << -1 << endl;
else
cout << f[ei][ej] << endl;
return 0;
}