#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
#include <string>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define fori(n) for (int i = 0; i < n; i++) 
#define forj(n) for (int j = 0; j < n; j++)

using namespace std;

int n;
int soln;
char cCube[10][10][10];
int iCube[10][10][10];
int a[3],b[3];

bool inb(int x, int y, int z) {
    if (x < 0 || y < 0 || z < 0 || x >= n || y >= n || z >= n) return false;
    else return true;
}

bool surround(int x, int y, int z, int s) {
    if(inb(x,y,z) && cCube[x][y][z] != '#') {
        if (iCube[x][y][z] > s) {
            iCube[x][y][z] = s;
            return true;
        }
    }
    return false;
}

void dumpi() {
    for (int z = 0; z < n; z++) {
        for (int y = 0; y < n; y++) {
            for (int x = 0; x < n; x++) {
                printf("%2d ",iCube[x][y][z]);
            }
            cout << endl;
        }
        cout << "---" << endl;
    }
}

int fill(int s) {
    //cout << "Searching for " << s-1 << endl;
    int ret = 0;
    for (int z = 0; z < n; z++) {
        for (int y = 0; y < n; y++) {
            for (int x = 0; x < n; x++) {
                if (iCube[x][y][z] == s-1) {
                    //cout << "Found a " << s-1 << endl;
                    for (int i = -1; i<= 1; i+=2) {
                        ret += surround(x+i,y,z,s);
                        ret += surround(x,y+i,z,s);
                        ret += surround(x,y,z+i,s);                        
                    }                    
                }
                
                if (iCube[b[0]][b[1]][b[2]] != 999) {
                    soln = s;
                    return -1;
                }
            }
        }
    }
    return ret;
}

int main() {
	ifstream in ("DATA5.txt");	// IS THIS THE RIGHT NUMBER?
	ofstream out ("OUT5.txt");  // IS THIS THE RIGHT NUMBER?

	for (int P = 0; P < 5; P++) {    
        in >> n;
        string line;
        getline(in,line);
        for (int z = 0; z < n; z++) {
            for (int y = 0; y < n; y++) {
                getline(in,line);
                for (int x = 0; x < n; x++) {
                    iCube[x][y][z] = 999;
                    cCube[x][y][z] = line[x];
                    if (line[x] == 'A') {
                        a[0] = x;
                        a[1] = y;
                        a[2] = z;
                        //printf("A: %d %d %d\n",x,y,z);
                    } else if (line[x] == 'B') {
                        b[0] = x;
                        b[1] = y;
                        b[2] = z;
                        //printf("B: %d %d %d\n",x,y,z);
                    }
                }
            }
        }
        
        iCube[a[0]][a[1]][a[2]] = 0;
        int res;
        
        for (int i = 1; i < n*n*n; i++) {
            res = fill(i);
            if (res == 0 || res == -1) break;
        }
        out << soln << endl;
        //dumpi();
        //cin.get();
    }
	return 0;
}

