#include <fstream>
#include <string>
#include <iostream>

using namespace std;
int main(int argc, char *argv[])
{
    ifstream data("DATA4.txt");
    ofstream out("OUT4.txt");
    for (int index0 = 0; index0 < 5;index0++) {
        string n[10], m[10];;
        for (int index = 0 ; index < 10; index++) {
            getline(data, n[index]);
            m[index] = n[index];
        }
        
        bool moved = true;
        bool tree = true;
        int sum = -1;
        while (moved == true) {
              sum++;
              tree = false;
              moved = false;
              //check
              for (int index2 = 0; index2 < 10; index2++) {
                       for(int index3 = 0; index3 < 10; index3++) {
                               if (n[index2][index3] == 'T') tree = true;
                               if (n[index2][index3] == 'F') {
                                                    if (index2 < 9 && n[index2 + 1][index3] == 'T') m[index2 + 1][index3] = 'F';
                                                    if (index2 > 0 && n[index2 - 1][index3] == 'T') m[index2 - 1][index3] = 'F';
                                                    if (index3 < 9 && n[index2][index3 + 1] == 'T') m[index2][index3 + 1] = 'F';
                                                    if (index3 > 0 && n[index2][index3 - 1] == 'T') m[index2][index3 - 1] = 'F';
                               }
                       }
              }
              for (int index2 = 0; index2 < 10; index2++) {
                       for(int index3 = 0; index3 < 10; index3++) {
                               if (m[index2][index3] != n[index2][index3]) moved = true;
                               n[index2][index3] = m[index2][index3];
                               //cout << n[index2][index3];
                       }
                       //cout << endl;
              }//system("PAUSE");
        }
        string buffer;
        getline(data, buffer);
        (tree == true) ? out << -1: out << sum;
        out << endl;
    }
    //system("PAUSE");
    return 0;
}

