import java.util.*;
import java.io.*;

public class dwite_4 {
	static Character[][] arr = new Character[10][10];

	public static void main(String[] args) throws IOException {
		File inputFile = new File("data4.txt");
		File outputFile = new File("out4.txt");
		BufferedReader br = new BufferedReader(new FileReader(inputFile));
		BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile)); 
		for (int i = 0; i < 5; i++) { // for 5 inputs
			// reads in grid
			for (int p = 0; p < 10; p++) {
				String s = br.readLine();
				for (int q = 0; q < 10; q++) {
					arr[p][q] = s.charAt(q);
				}
			}
			// records change
			boolean change = true;
			// total time
			int time = 0;
			// temporary to store new values
			Character[][] temp = new Character[10][10];
			while (change && Tree()) { // while the fire does not stop spreading
				duplicate(temp); // old array
				change = false;
				for (int r = 0; r < 10; r++) {
					for (int c = 0; c < 10; c++) {
						// is fire
						if (temp[r][c] == 'F') {
							if (r + 1 <= 9) {
								if (arr[r + 1][c] == 'T') {
									arr[r + 1][c] = 'F';
									change = true;
								}
							}

							if (r - 1 >= 0) {
								if (arr[r - 1][c] == 'T') {
									arr[r - 1][c] = 'F';
									change = true;
								}
							}

							if (c + 1 <= 9) {
								if (arr[r][c + 1] == 'T') {
									arr[r][c + 1] = 'F';
									change = true;
								}
							}

							if (c - 1 >= 0) {
								if (arr[r][c - 1] == 'T') {
									arr[r][c - 1] = 'F';
									change = true;
								}
							}
						} // if fire
					}
				}
				time++;
			}
			if (change) {
				System.out.println(time);
				bw.write(time+""); 
			} else {
				System.out.println(-1);
				bw.write(-1+"");
			}
			if (i!=4)
			bw.newLine();
			
			// separation
			br.readLine();
		}
		bw.close(); 
	} // main

	public static void duplicate(Character[][] temp) {
		for (int p = 0; p < 10; p++) {
			for (int q = 0; q < 10; q++) {
				temp[p][q] = arr[p][q];
			}
		}
	}

	public static boolean Tree() {
		boolean tree = false;
		for (int p = 0; p < 10; p++) {
			for (int q = 0; q < 10; q++) {
				if (arr[p][q] == 'T')
					return true;
			}
		}
		return tree;
	}
}

