#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>

using namespace std;

char map[10][10];

int main(){
    freopen("DATA4.txt", "r", stdin);
    freopen("OUT4.txt", "w", stdout);

    for(int case_count=0;case_count<5;case_count++){
        queue<int> Qx;
        queue<int> Qy;
        queue<int> Qd;
        int max = 0;
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                scanf(" %c", &map[i][j]);
                if(map[i][j] == 'F'){
                    Qx.push(j);
                    Qy.push(i);
                    Qd.push(0);
                    map[i][j] = 'T';
                }
            }
        }

        while(!Qd.empty()){
            int x, y, d;
            x = Qx.front(); Qx.pop();
            y = Qy.front(); Qy.pop();
            d = Qd.front(); Qd.pop();

            if(x < 0 or x >= 10 or y < 0 or y >= 10 or map[y][x] != 'T'){
                continue;
            }

            map[y][x] = 'F';
            Qx.push(x-1);
            Qy.push(y);
            Qd.push(d+1);
            Qx.push(x+1);
            Qy.push(y);
            Qd.push(d+1);
            Qx.push(x);
            Qy.push(y-1);
            Qd.push(d+1);
            Qx.push(x);
            Qy.push(y-1);
            Qd.push(d+1);

            max = d;
        }

        bool alive = false;
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                if(map[i][j] == 'T') alive = true;
            }
        }

        if(alive) printf("-1\n");
        else printf("%d\n", max);

        char fuck;
        for(int i=0;i<11;i++){
            scanf("%c", &fuck);
        }
    }
}

