inf = open("DATA4.txt")
out = open("OUT4.txt","w")
x = 0
y = 1
grid = []
points = []

for sets in range(5):
    c = 0
    for line in range(10):
        n = inf.readline().strip("\n")
        l = list(n)
        grid.append(l)
        if "F" in l:
            for each in range (len(l)):
                if l[each] == "F":
                    points.append((line,each))
    
    bad = False             
    fl = False
    while fl==False:
        c +=1
        fl = True
        add=[]
        for each in points:
            if each[x] < 9 and grid[each[x]+1][each[y]]=="T":
                p = (each[x]+1,each[y])
                if p not in points:
                    add.append(p)
                fl = False
                grid[each[x]+1][each[y]] = "F"
##                print "HI"
            if  each[x] > 0 and grid[each[x]-1][each[y]]=="T":
                p = (each[x]-1,each[y])
                if p not in points:
                    add.append(p)
                fl = False
                grid[each[x]-1][each[y]] = "F"
##                print "BYE"
            if each[y] < 9 and grid[each[x]][each[y]+1]=="T":
                p = (each[x],each[y]+1)
                if p not in points:
                    add.append(p)
                fl = False
                grid[each[x]][each[y]+1] = "F"
##                print"rage"
            if each[y] > 0 and grid[each[x]][each[y]-1]=="T":
                p = (each[x],each[y]-1)
                if p not in points:
                    add.append(p)
                fl = False
                grid[each[x]][each[y]-1] = "F"
##                print"QUIT"
        points+=add
        
    for each in grid:
        if "T" in each:
            bad = True
    if bad:
        out.write("-1"+"\n")
    else:
        out.write(str(c-1)+"\n")
    inf.readline()

    grid = []
    points = []
inf.close()
out.close()

