#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;
bool inb(int r, int c) {
	return (r >= 0 && r < 5 && c >= 0 && c < 5);
}
int main() {
	ifstream in ("DATA5.txt");	// IS THIS THE RIGHT NUMBER?
	ofstream out ("OUT5.txt");  // IS THIS THE RIGHT NUMBER?
	int f;
	bool done;
	string tmp;
	for (int Z = 0; Z < 5; Z++) {
		done = false;
		in >> f; 
		getline(in,tmp);
		string maze[f][5];
		int step[f][5][5];
		fori(f){
			forj(5){
				getline(in, maze[i][j]);
			}
		}
		
		fori(f){
			forj(5){
				for(int k=0; k<5; k++){
					if(i == 0 && maze[i][j][k] == 'A') step[i][j][k] = 0;
					else step[i][j][k] = 9;
				}
			}
		}
		//cout << "done reading" << endl;
		for(int l=0; l<f; l++){
			fori(5){
				if(done) break;
				forj(5){
					if(done) break;
					if(step[l][i][j] != 9){
						if(maze[l+1][i][j] != '#'){
							 if(maze[l+1][i][j] == 'B'){
								out << l+1 << endl;
								done = true;
								break;
							}
							else step[l+1][i][j] = 0;
						}
						if(inb(i+1,j) && maze[l+1][i+1][j] != '#'){
							 if(maze[l+1][i+1][j] == 'B'){
								out << l+1 << endl;
								done = true;
								break;
							}
							else step[l+1][i+1][j] = 0;
						}
						if(inb(i-1,j) && maze[l+1][i-1][j] != '#'){
							 if(maze[l+1][i-1][j] == 'B'){
								out << l+1 << endl;
								done = true;
								break;
							}
							else step[l+1][i-1][j] = 0;
						}
						if(inb(i,j+1) && maze[l+1][i][j+1] != '#'){
							 if(maze[l+1][i][j+1] == 'B'){
								out << l+1 << endl;
								done = true;
								break;
							}
							else step[l+1][i][j+1] = 0;
						}
						if(inb(i,j-1) && maze[l+1][i][j-1] != '#'){
							 if(maze[l+1][i][j-1] == 'B'){
								out << l+1 << endl;
								done = true;
								break;
							}
							else step[l+1][i][j-1] = 0;
						}
					}
				}
			}
		}
	}
	return 0;
}
		

