input_file = open("DATA4.txt", "r")
output_file = open("OUT4.txt", "w")
for a in range(0,5,1):
    forest = [[".",".",".",".",".",".",".",".",".",".",".","."]]
    for b in range(0,10,1):
        line = input_file.readline().rstrip("\n")
        line_array = ["."]
        for c in line:
            line_array.append(c)
        line_array.append(".")
        forest.append(line_array)
    forest.append([".",".",".",".",".",".",".",".",".",".",".","."])
    fire = True
    time = 0
    while (fire):
        fire = False
        for b in range(1,11,1):
            for c in range(1,11,1):
                if (forest[b][c] == "F"):
                    forest[b][c] = "."
                    if (forest[b-1][c] == "T" or forest[b-1][c] == "NF"):
                        forest[b-1][c] = "NF"
                        fire = True
                    if (forest[b+1][c] == "T" or forest[b+1][c] == "NF"):
                        forest[b+1][c] = "NF"
                        fire = True
                    if (forest[b][c-1] == "T" or forest[b][c-1] == "NF"):
                        forest[b][c-1] = "NF"
                        fire = True
                    if (forest[b][c+1] == "T" or forest[b][c+1] == "NF"):
                        forest[b][c+1] = "NF"
                        fire = True
        has_t = False
        more_time = False
        for b in range(1,11,1):
            for c in range(1,11,1):
                if (forest[b][c] == "NF"):
                    forest[b][c] = "F"
                    more_time = True
                if (forest[b][c] == "T"):
                    has_t = True
        if more_time:
            time = time + 1
    if has_t:
        time = -1
    output_file.write(str(time) + "\n")
    line = input_file.readline().rstrip("\n")
input_file.close()
output_file.close()

