import java.io.*;

public class S3
{
    static int[] [] steps;
    static int endX;
    static int endY;

    public static void main (String[] args) throws IOException
    {
	BufferedReader input = new BufferedReader (new FileReader ("DATA3.txt"));
	PrintWriter output = new PrintWriter (new FileWriter ("OUT3.txt"));

	int numLines = 10;
	String[] data = new String [numLines];
	for (int i = 0 ; i < numLines ; i++)
	{
	    data [i] = input.readLine ();
	}

	char[] [] map = new char [10] [data [0].length ()];
	for (int i = 0 ; i < data.length ; i++)
	{
	    for (int j = 0 ; j < data [i].length () ; j++)
	    {
		map [i] [j] = data [i].charAt (j);
	    }
	}
	//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++)
	    {
		sum += startFind (map, in [j].charAt (i), in [j].charAt (i + 1));

	    }
	    output.println (sum);
	}

	char startChar = 'B';
	char targetChar = 'F';

	output.close ();
    }


    static void find (char[] [] map, int x, int y, char target, int counter)
    {
	if (map [x] [y] == '#')
	    return;
	if (counter < steps [x] [y])
	    steps [x] [y] = counter;
	else
	    return;
	// printOutArray (steps);
	if (map [x] [y] == target)
	{
	    endX = x;
	    endY = y;
	}
	//   steps [x] [y] = counter;

	if (x > 0 && (map [x - 1] [y] != '#'))
	    find (map, x - 1, y, target, counter + 1);
	if (x < map.length - 1 && (map [x + 1] [y] != '#'))
	    find (map, x + 1, y, target, counter + 1);
	if (y > 0 && (map [x] [y - 1] != '#'))
	    find (map, x, y - 1, target, counter + 1);
	if (y < map [0].length - 1 && (map [x] [y + 1] != '#'))
	    find (map, x, y + 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];
	for (int i = 0 ; i < steps.length ; i++)
	{
	    for (int j = 0 ; j < steps [i].length ; j++)
	    {
		steps [i] [j] = 999;
	    }
	}

	int startX = 0;
	int startY = 0;
	for (int i = 0 ; i < map.length ; i++)
	{
	    for (int j = 0 ; j < map [i].length ; j++)
	    {
		if (map [i] [j] == start)
		{
		    startX = i;
		    startY = j;
		    find (map, i, j, target, 0);
		}
	    }
	}
	return steps [endX] [endY];
    }


    static void printOutArray (int[] [] 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 ();
	}
    }
}



