import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

/*
 * Forest fires are really dangerous, and can be started by even the smallest flame. 
 * Spreading from tree to tree, fires can engulf an entire forest in a matter of weeks. 
 * Given a map of a forest with locations of where a fire (or multiple fires) might have 
 * started, determine how long it would take the fire to capture the entire forest. 

 The input file DATA4.txt will contain 5 test cases, each a 10x10 map, followed by a line of 
 10 dashes for visual separation. 

 . - blank space
 T - a tree
 F - a tree on fire
 Fires only spread from existing fire to adjacent trees, in 4 directions (so not diagonally). 
 It takes 1 unit of time for the fire to spread from one location to the next. The fire spreads
 in all 4 directions at the same time. 

 The output file OUT4.txt will contain 5 lines, the time it takes for the fire to capture 
 the entire forest. If some piece of the forest survives, output -1. 
 */
public class Problem1
{
	public static final int BLANK = 1;
	public static final int TREE = 2;
	public static final int FIRE = 3;

	public static int Forest[][] = new int[10][10];
	Vector<Point> Fires = new Vector<Point>();

	public Problem1() throws IOException
	{
		BufferedReader br = new BufferedReader(new FileReader("DATA4.txt"));

		while (br.ready())
		{
			for (int i = 0; i < 10; i++)
			{
				String str = br.readLine();
				for (int j = 0; j < str.length(); j++)
				{
					char c = str.charAt(j);

					switch (c)
					{
						case '.':
							Forest[i][j] = BLANK;
						break;

						case 'T':
							Forest[i][j] = TREE;
						break;

						case 'F':
							Forest[i][j] = FIRE;
							Fires.add(new Point(i, j));
						break;

						default:
						break;
					}
				}
			}
			br.readLine(); // reads in the ------
			DoStuff();
			Fires.clear();
		}
	}

	public void DoStuff()
	{
		int TimeUnits = 0;
		int NumTreesAddedThisTimeUnit = 0;
		do
		{
			NumTreesAddedThisTimeUnit = 0;
			for (int i = 0; i < Fires.size(); i++)
			{
				for (int x = -1; x <= 1; x++)
				{
					for (int y = -1; y <= 1; y++)
					{
						if (x == -1 && (y == -1 || y == 1))//diagonally
							continue;
						if (x == 1 && (y == -1 || y == 1))//diagonally
							continue;
						if (x == 0 && y == 0) //centre piece 
							continue;

						Point p = Fires.get(i);
						int Fx = p.x;
						int Fy = p.y;

						//System.out.println(Fx + ", " + Fy + "\t " + (Fx + x) + "\t" + (Fy + y));

						if (Forest[Fx + x][Fy + y] == TREE) //burn!
						{
							Forest[Fx + x][Fy + y] = FIRE;
							NumTreesAddedThisTimeUnit++;
						}
					}
				}
				//System.out.println("");
			}

			Scan();

			if (NumTreesAddedThisTimeUnit > 0)
				TimeUnits++;
		} 
		while (NumTreesAddedThisTimeUnit > 0);
		PrintOut();
		if (Scan() > 0)
			TimeUnits = -1;
		System.out.println(TimeUnits);
		try
		{
			BufferedWriter bw = new BufferedWriter(new FileWriter("OUT4.txt", true));
			bw.write(String.valueOf(TimeUnits));
			bw.newLine();
			bw.close();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int Scan()
	{
		int NumTrees = 0;
		Fires.clear();
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 10; j++)
			{
				if (Forest[i][j] == FIRE)
					Fires.add(new Point(i, j));

				if (Forest[i][j] == TREE)
					NumTrees++;
			}
		}

		return NumTrees;
	}

	public void PrintOut()
	{
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 10; j++)
			{
				switch (Forest[i][j])
				{
					case BLANK:
						System.out.print(".");
					break;

					case TREE:
						System.out.print("T");
					break;

					case FIRE:
						System.out.print("F");
					break;

					default:
					break;
				}
			}
			System.out.println();
		}
	}

	public static void main(String[] args)
	{

		try
		{
			new Problem1();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

