import java.io.*;

public class S5
{
    static int[] [] [] steps;
    static int endX;
    static int endY;
    static int endZ;

    public static void main (String[] args) throws IOException
    {
	BufferedReader input = new BufferedReader (new FileReader ("DATA5.txt"));
	PrintWriter output = new PrintWriter (new FileWriter ("OUT5.txt"));
	for (int numRun = 1 ; numRun <= 5 ; numRun++)
	{
	    int numLines = Integer.parseInt (input.readLine ());
	    String[] data = new String [numLines];


	    char[] [] [] map = new char [numLines] [numLines] [numLines];
	    for (int i = 0 ; i < numLines ; i++)
	    {
		for (int a = 0 ; a < numLines ; a++)
		{
		    data [a] = input.readLine ();
		}
		for (int j = 0 ; j < numLines ; j++)
		{
		    for (int k = 0 ; k < numLines ; k++)
		    {
			map [i] [j] [k] = data [j].charAt (k);
		    }
		}
	    }
	    //printOutArray (map);

	    /*String[] in = new String [5];
	    for (int i = 0 ; i < 5 ; i++)
	    {
		in [i] = input.readLine ();
	    }*/

	    //------------------START--------------//
	    //int sum = 0;
	    /*for (int j = 0 ; j < in.length ; j++)
	    {
		//sum = 0;
		for (int i = 0 ; i < in [j].length () - 1 ; i++)
		{*/
	    startFind (map, 'A', 'B');

	    /* }
	     output.println (sum);
	    }*/

	    /* char startChar = 'B';
	     char targetChar = 'F';*/
	    output.println (steps [endX] [endY] [endZ]);
	}
	output.close ();
    }



    static void find (char[] [] [] map, int x, int y, int z, char target, int counter)
    {
	if (map [x] [y] [z] == '#')
	    return;
	if (counter < steps [x] [y] [z])
	    steps [x] [y] [z] = counter;
	else
	    return;
	// printOutArray (steps);
	if (map [x] [y] [z] == target)
	{
	    endX = x;
	    endY = y;
	    endZ = z;
	}
	//   steps [x] [y] = counter;

	if (x > 0 && (map [x - 1] [y] [z] != '#'))
	    find (map, x - 1, y, z, target, counter + 1);
	if (x < map.length - 1 && (map [x + 1] [y] [z] != '#'))
	    find (map, x + 1, y, z, target, counter + 1);
	if (y > 0 && (map [x] [y - 1] [z] != '#'))
	    find (map, x, y - 1, z, target, counter + 1);
	if (y < map [0].length - 1 && (map [x] [y + 1] [z] != '#'))
	    find (map, x, y + 1, z, target, counter + 1);
	if (z < map [0].length - 1 && (map [x] [y] [z + 1] != '#'))
	    find (map, x, y, z + 1, target, counter + 1);
	if (z > 0 && (map [x] [y] [z - 1] != '#'))
	    find (map, x, y, z - 1, target, counter + 1);
    }


    static void printOutArray (char[] [] array)
    {
	for (int i = 0 ; i < array.length ; i++)
	{
	    for (int j = 0 ; j < array [i].length ; j++)
	    {
		System.out.print (array [i] [j]);
	    }
	    System.out.println ();
	}
    }


    static int startFind (char[] [] [] map, char start, char target)
    {
	steps = new int [map.length] [map [0].length] [map [0] [0].length];
	for (int i = 0 ; i < steps.length ; i++)
	{
	    for (int j = 0 ; j < steps [i].length ; j++)
	    {
		for (int k = 0 ; k < steps [i] [j].length ; k++)
		{
		    steps [i] [j] [k] = 999;
		}
	    }
	}

	int startX = 0;
	int startY = 0;
	int startZ = 0;

	for (int i = 0 ; i < map.length ; i++)
	{
	    for (int j = 0 ; j < map [i].length ; j++)
	    {
		for (int k = 0 ; k < steps [i] [j].length ; k++)
		{
		    if (map [i] [j] [k] == start)
		    {
			startX = i;
			startY = j;
			startZ = k;
			find (map, i, j, k, target, 0);
		    }
		}
	    }
	}
	return steps [endX] [endY] [endZ];
    }


    static void printOutArray (char[] [] [] array)
    {
	for (int i = 0 ; i < array.length ; i++)
	{
	    for (int j = 0 ; j < array [i].length ; j++)
	    {
		for (int k = 0 ; k < array [i] [j].length ; k++)
		    System.out.print (array [i] [j] [k]);
		System.out.println ();
	    }
	    System.out.println ();
	}
    }
}



