def find_fires():
    pos = []
    for y in range(10):
        for x in range(10):
            if grid[y][x] == "F":
                visited[y][x] = True
                pos.append([x,y])
    return pos

def next_pos(top):
    x,y = top
    spots = []
    for pos in [[x+1,y], [x-1,y], [x,y+1], [x,y-1]]:
        x2,y2 = pos
        if x2 < 0 or x2 >= 10 or y2 < 0 or y2 >= 10: continue
        if visited[y2][x2]: continue
        if grid[y2][x2] == "T":
            visited[y2][x2] = True
            spots.append(pos)
    return spots

def bfs(queue):
    t = 0
    next_lv = []
    while len(queue) != 0:
        top = queue.pop(0)
        next_lv.extend(next_pos(top))
        
        if len(queue) == 0:
            queue = list(next_lv)
            next_lv = []
            t += 1
    return t

def found_all():
    for y in range(10):
        for x in range(10):
            if grid[y][x] == "T" and not visited[y][x]: return False
    return True
    
data = open("DATA4.txt")
out = open("OUT4.txt","w")
for case in range(5):
    grid = []
    for row in range(10):
        grid.append(data.readline().strip())
    
    visited = [[False]*10 for z in range(10)]
    pos = find_fires()
    t = bfs(pos)-1
    if not found_all():
        out.write("-1\n")
    else:
        out.write(str(t)+"\n")
    data.readline()
