f = open("DATA4.txt")
o = open("OUT4.txt", "w")
global m, c, trees, fires

def fire(coords):
    global m, c, fires, trees
    y = coords[0]
    x = coords[1]

    if m[y][x]!='T':
        return
    m[y][x]='F'
    fires.append(coords)
    trees-=1
    
def floodfire(coords):
    y = coords[0]
    x = coords[1]
    
    if x:
        fire((y,x-1))
    if y:
        fire((y-1,x))
    if (9-x):
        fire((y,x+1))
    if (9-y):
        fire((y+1,x))


for q in range(5):
    m = [list(f.readline()[:-1]) for x in range(11)][:-1]
    t = 0
    trees = 0
    fires = []
    for y, row in enumerate(m):
        for x, tile in enumerate(row):
            if tile == 'T':
                trees+=1
            if tile == 'F':
                fires.append((y,x))

    while trees:
        c = fires[:]
        fires = []
        t+=1
        if not c:
            t = -1
            break
        while c:
            floodfire(c.pop(0))

    o.write("%d\n" % t)

o.close()
        


