import java.io.*;
import java.util.*;

class Problem5 {

	private static int[][][] map;
	private static int w;
	public static void main (String[] args) throws IOException {

		FileReader reader = new FileReader("DATA5.txt");
		FileWriter fileWriter = new FileWriter("OUT5.txt");
		BufferedReader br = new BufferedReader(reader);
		BufferedWriter writer = new BufferedWriter(fileWriter);
		Scanner scan = new Scanner(System.in);



		String tmp;
		Queue<Loc> locations = new LinkedList<Loc>();
		Loc tLoc, aLoc = null, bLoc = null;

		for(int n = 0; n < 5; n ++) {

			w = Integer.parseInt(br.readLine());
			map = new int[w][w][w];

			for (int z = 0; z < w; z++) {
				for (int y = 0; y < w ; y++) {
					tmp = br.readLine();
					for(int x = 0; x < w; x++) {
						if (tmp.charAt(x) == 'A') {
							aLoc = new Loc(x, y, z);
							map[z][y][x] = 0;
						} else if (tmp.charAt(x) == 'B') {
							bLoc = new Loc(x, y, z);
							map[z][y][x] = 99;
						} else if (tmp.charAt(x) == '.') {
							map[z][y][x] = 99;
						} else if (tmp.charAt(x) == '#') {
							map[z][y][x] = -1;
						} else {
							System.out.println("error");
						}
					}
				}
			}

			locations.offer(aLoc);

			while((tLoc = locations.poll()) != null) {

				if (isValid(tLoc.x - 1, tLoc.y, tLoc.z)) {
					map[tLoc.z][tLoc.y][tLoc.x - 1] = map[tLoc.z][tLoc.y][tLoc.x] + 1;
					locations.offer(new Loc(tLoc.x - 1, tLoc.y, tLoc.z));

				} else if (isValid(tLoc.x + 1, tLoc.y, tLoc.z)) {
					map[tLoc.z][tLoc.y][tLoc.x + 1] = map[tLoc.z][tLoc.y][tLoc.x] + 1;
					locations.offer(new Loc(tLoc.x + 1, tLoc.y, tLoc.z));


				} else if (isValid(tLoc.x, tLoc.y + 1, tLoc.z)) {
					map[tLoc.z][tLoc.y + 1][tLoc.x] = map[tLoc.z][tLoc.y][tLoc.x] + 1;
					locations.offer(new Loc(tLoc.x, tLoc.y + 1, tLoc.z));

				} else if (isValid(tLoc.x, tLoc.y - 1, tLoc.z)) {
					map[tLoc.z][tLoc.y - 1][tLoc.x] = map[tLoc.z][tLoc.y][tLoc.x] + 1;
					locations.offer(new Loc(tLoc.x, tLoc.y - 1, tLoc.z));

				} else if (isValid(tLoc.x, tLoc.y, tLoc.z + 1)) {
					map[tLoc.z + 1][tLoc.y][tLoc.x] = map[tLoc.z][tLoc.y][tLoc.x] + 1;
					locations.offer(new Loc(tLoc.x, tLoc.y, tLoc.z + 1));

				} else if (isValid(tLoc.x, tLoc.y, tLoc.z - 1)) {
					map[tLoc.z - 1][tLoc.y][tLoc.x] = map[tLoc.z][tLoc.y][tLoc.x] + 1;
					locations.offer(new Loc(tLoc.x, tLoc.y, tLoc.z - 1));
				}
				//System.out.println("Inside the queue");
				print();
				scan.nextLine();
			}

			System.out.println("I reach here");

			writer.write(Integer.toString( map[bLoc.z][bLoc.y][bLoc.x]));
			writer.newLine();

		}

		writer.flush();

	}

	private static boolean isValid(int x, int y, int z) {
		boolean rc = false;
		if (z >= 0 && z < w && x >= 0 && x < w && y >= 0 && y < w && map[z][y][x] == 99)
			//System.out.println("int [" + z "][" + + "][" + y + )
			rc = true;

		return rc;

	}

	private static void print() {
		for (int z = 0; z < w; z++) {
			for (int y = 0; y < w ; y++) {
				for(int x = 0; x < w; x++) {
					System.out.print(map[z][y][x] + " ");
				}
				System.out.println();
			}

		}

	}

}

class Loc {
	public int x, y, z;

	public Loc (int nx, int ny, int nz) {
		x = nx;
		y = ny;
		z = nz;
	}

	public Loc (Loc aloc) {
		x = aloc.x;
		y = aloc.y;
		z = aloc.z;
	}

}


