#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;
#define forr(a,b,c) for(int (a) = (b); (a) < (c); (a)++)
#define fore(a,b,c) for(int (a) = (b); (a) <= (c); (a)++)
#define forx(a,b,c) for(int (a) = (b); (a) > (c); (a)--)
int main ()
{
    ifstream in;
    in.open("DATA4.txt");
    ofstream out;
    out.open("OUT4.txt");
    string n[10];
    int c[10][10],depth,count,prev;
    forr (q,0,5)
    {
         count = 0;
         depth = 0;
         forr (i,0,10)
              getline (in,n[i]);
         forr (i,0,10)
              forr (j,0,10)
                   if (n[i][j] == '.')
                      c[i][j] = -2;
                   else if (n[i][j] == 'T')
                   {
                      count++;
                      c[i][j] = -1;
                   }
                   else if (n[i][j] == 'F')
                   {
                      c[i][j] = 0;
                   }
         while (count > 0)
         {
               prev = count;
               forr (i,0,10)
                    forr (j,0,10)
                         if (c[i][j] == depth)
                         {
                            if (i < 9)
                               if (c[i+1][j] == -1)
                               {
                                  c[i+1][j] = depth+1;
                                  count--;
                               }
                            if (i > 0)
                               if (c[i-1][j] == -1)
                               {
                                  c[i-1][j] = depth+1;
                                  count--;
                               }
                            if (j < 9)
                               if (c[i][j+1] == -1)
                               {
                                  c[i][j+1] = depth+1;
                                  count--;
                               }
                            if (j > 0)
                               if (c[i][j-1] == -1)
                               {
                                  c[i][j-1] = depth+1;
                                  count--;
                               }
                         }
               depth++;
               if (prev == count)
                  break;
         }
         getline(in,n[0]);
         if (count == 0)
            out << depth << endl;
         else out << "-1\n";
    }
    return 0;
}

