
/**
 * Write a description of class basic here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

import java.io.*;
public class basic
{
                public static FileOutputStream out;
                public static PrintStream p;
                public static int[][][] best;
                public static char[][][] grid;
                public static int bestReal;
                public static int intx;
                public static int  inty;
                public static int intz;
                public static int numSize;
                
                public static void main(String[] args)
                {
                    try
                    {
                    out = new FileOutputStream("OUT5.txt");
                    p = new PrintStream(out);
                    FileInputStream fstream = new FileInputStream("DATA5.txt");
                    DataInputStream in = new DataInputStream(fstream);
                    
                    for ( int num = 0; num < 5; num++)
                    {
                        bestReal = 10000;
                        numSize = Integer.parseInt(in.readLine());
                        grid = new char [numSize][numSize][numSize];
                        best = new int [numSize][numSize][numSize];
                        for ( int i = 0; i < numSize; i++)
                        {
                        for (int i2 = 0; i2 < numSize; i2++)
                        {
                            String line = in.readLine();
                            for (int i3 = 0; i3 < numSize; i3++)
                            {
                                grid[i][i2][i3] = line.charAt(i3);
                                best[i][i2][i3] = 10000;
                                if (grid[i][i2][i3] == 'A')
                                {
                                    intx = i;
                                    inty = i2;
                                    intz = i3;
                                }
                            }
                        }
                    }
                    move(intx, inty, intz, 0);
                    if (num < 4)
                    p.println(Integer.toString(bestReal));
                    if (num == 4)
                    p.print(Integer.toString(bestReal));
                }
                }
                catch(IOException e)
                {
                    System.out.println(e);
                }

					
                }
                public static void move(int x,int y,int z,int time)
                {
                    if (x < numSize
                    && y < numSize
                    && z < numSize
                    && x >= 0
                    && y >= 0
                    && z >= 0)
                    {
                        //System.out.println(x+""+y+""+z);
                    if (time < best[x][y][z])
                        if (grid[x][y][z]!= '#')
                        {
                            best[x][y][z] = time;
                            move (x+1,y,z,time+1);
                            move (x-1,y,z,time+1);
                            move (x,y+1,z,time+1);
                            move (x,y-1,z,time+1);
                            move (x,y,z+1,time+1);
                            move (x,y,z-1,time+1);
                            if (grid[x][y][z] == 'B')
                            {
                                bestReal = time;
                            }
                        }
                    }
                       
                }
}

