#include<fstream>
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<sstream>
#include<stdlib.h>
#include<map>
#include<vector>
#include<set>
#include<cstring>
using namespace std;

string f[10];
int t[10][10];
bool vis[10][10];
int x[4] = {1,0,-1,0};
int y[4] = {0,1,0,-1};
int n, maxV;
bool ok(int i, int j)
{
	return (i>=0 && j>=0 && i<n && j<n && f[i][j]=='T' && !vis[i][j]);
}
bool ok2(int i, int j)
{
	return (i>=0 && j>=0 && i<n && j<n);
}
void burnTrees(int i, int j)
{
//	cout << i << " " << j << endl;
//	getchar();
	int a, minCost = t[i][j];
	vis[i][j] = true;
	for(a=0;a<4;a++)
		if(ok2(i+x[a],j+y[a]))
			minCost = min(minCost, t[i+x[a]][j+y[a]]+1);
	t[i][j] = minCost;
//	cout << t[i][j] << endl;
	for(a=0;a<4;a++)
		if(ok(i+x[a],j+y[a]))
			burnTrees(i+x[a],j+y[a]);
}
void print()
{
	int i, j;
	for(i=0;i<n;i++)	
	{
		for(j=0;j<n;j++)
		if(t[i][j] < 100)
			cout << t[i][j] << " ";
		else
			cout << "* ";
		cout << endl;
	}
}
int main()
{
 	ifstream fin ("DATA4.txt");
 	ofstream fout ("OUT4.txt");
 	int tests = 5,i,j;
 	n = 10;
 	while(tests--)
 	{
		for(i=0;i<n;i++)	
			fin >> f[i];
		memset(t,63,sizeof t);
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				if(f[i][j]=='F')
				{
					memset(vis,0,sizeof vis);
					t[i][j] = 0;
//					cout << "herre" << endl;
					burnTrees(i,j);
//					print();
			}
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				if(f[i][j]=='T')
					if(t[i][j]<100)
					{
						maxV = max(maxV,t[i][j]);
					}
					else
					{
						maxV = -1;
						break;
					}
		fout << maxV << endl;
	}
//	system("pause");
	return 0;
}

