/*
ID: divad151
PROG: pname
LANG: C++
*/
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <queue>
#include <cassert>

using namespace std;

//------------------------------------------------------------------------------------------------------------------------
// CONSTANTS
//------------------------------------------------------------------------------------------------------------------------

#define PI          3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803   /* all from memory! */
#define PHI         1.6180339887498948482045868343656   /* = (sqrt(5)+1)/2  */
#define MAXINT      0x7fffffff                          /* for a signed integer */
#define EPSILON     0.00000001                          /* for floating-point value comparison */
#define MAXDOUBLE   1.79769313486231570e+308

#define NORTH       0
#define EAST        1
#define SOUTH       2
#define WEST        3

#define MS(a,b)     memset(a, b, sizeof(a))
#define FOR(a,b,c)  for (a = (b); a < (c); ++a)

const int fact[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600 }; // up to 12!
const int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  // lengths of each month


//------------------------------------------------------------------------------------------------------------------------
// GLOBAL VARIABLES
//------------------------------------------------------------------------------------------------------------------------

int dx[] = {  0,  1,  0, -1, 0 , 0};
int dy[] = { -1,  0,  1,  0,  0, 0};
int dz[] = { 0,   0,  0,  0, 1, -1};
int cmax = -MAXINT;
int cmin = MAXINT;


//------------------------------------------------------------------------------------------------------------------------
// FUNCTIONS/CLASS DEFINITIONS
//------------------------------------------------------------------------------------------------------------------------


struct Point
{
    int x, y,z;
    Point () {}
    Point (int X, int Y,int Z)
    {
        x = X;
        y = Y;
        z = Z;
    }
    void out()
    {
        cout << "(" << x << ", " << y << ")";
    }
};

int n;

bool inBounds(int x, int y,int z)
{
	return (x >= 0 && y >= 0 && z >= 0 && x < n && y <n && z < n);
}



//------------------------------------------------------------------------------------------------------------------------
// MAIN PROGRAM EXECUTION
//------------------------------------------------------------------------------------------------------------------------
int main()
{
    ifstream fin("data5.txt");
    ofstream fout("out5.txt");
    int i = 0, j = 0, k = 0, l;
    int num, n1, n2, n3;
    string str;
    char c;

    char grid[10][10][10];

    for (i = 0; i < 5; ++i)
    {
        int dist[10][10][10];
        memset(dist,-1,sizeof(dist));
        fin >> n;
        Point start, end;
        for (j = 0; j < n; ++j)
        {
            for (k = 0; k < n;++k)
            {
                for (l=0;l<n;++l)
                {
                    fin >> c;
                    grid[j][k][l]=c;
                    if (c=='A')start = Point(j,k,l);
                    if(c=='B')end=Point(j,k,l);
                }
            }
        }

        queue <Point> Q;
        Q.push(start);
        dist[start.x][start.y][start.z] = 0;
        while (!Q.empty())
        {
            Point cur = Q.front();
            Q.pop();
            for (j = 0; j < 6; ++j)
            {
                Point next(cur.x+dx[j],cur.y+dy[j],cur.z+dz[j]);
                if (!inBounds(next.x,next.y,next.z))continue;
                if (dist[next.x][next.y][next.z]==-1 && grid[next.x][next.y][next.z] != '#')
                {
                    dist[next.x][next.y][next.z] = dist[cur.x][cur.y][cur.z]+1;
                    Q.push(next);
                }
            }
        }

        fout << dist[end.x][end.y][end.z] << endl;



    }










    return 0;
}




