import java.io.*;
import java.util.*;

public class S5 {

	/**
	 * @param args
	 */
    static int number[][][];
    static char [][][] grid;
    static int t;
    static int starty,startx;
    static int step;
	public static void main(String[] args) throws IOException {
		 Scanner in= new Scanner(new File("DATA5.txt"));
		 PrintWriter pw = new PrintWriter(new FileWriter("OUT5.txt"));
		 String temp2;
         for (int i=0;i<5;i++){
        	 t=Integer.parseInt(in.nextLine());
        	 grid = new char [t][5][5];
        	 number = new int[t][5][5];
        	 	for (int j=0;j<t;j++){
        	 		for (int k=0;k<5;k++){
            	 		temp2= in.nextLine();
            	 			for (int l=0;l<5;l++){
            	 				grid[j][k][l] = temp2.charAt(l);
            	 					if (grid[0][k][l]=='A')
            	 					{
            	 						starty=k;
            	 						startx=l;
            	 						number[j][k][l]=t;
            	 					}
            	 					if (grid[j][k][l]=='#')
            	 					{
            	 						number[j][k][l]=-1;
            	 					}
            	 					if (grid[j][k][l]=='.' || grid[j][k][l]=='B' )
            	 					{
            	 						number[j][k][l]=t;
            	 					}
            	 			}
            	 			}
                          } 
        	 	recurse(0,starty,startx,0);
        	 	pw.println(step);
         }
         pw.close();
	}

    public static void recurse(int time ,int y,int x, int count){
            if (time >= t || time < 0 || y >= 5 || y < 0 || x >= 5 || x < 0)
                    return;
            if (number[time][y][x] == -1)
                    return;
            if (count > number[time][y][x])
                return;
            else{
            	number[time][y][x]=count;
                  	if (grid[time][y][x] == 'B'){
                    step=number[time][y][x];
                    return;}
                    	
            }        
            recurse(time+1,y,x,count+1);
            recurse(time+1,y+1,x,count+1);
            recurse(time+1,y-1,x,count+1);
            recurse(time+1,y,x+1,count+1);
            recurse(time+1,y,x-1,count+1);
    }
}

