### BUGGY CODE ###

def main():
    h, w = map(int, input().split())
    s = [input() for _ in [0]*h]
    g = [dict() for _ in [0]*(h+w+2)]

    for i in range(h):
        for j in range(w):
            if s[i][j] == "S":
                g[0][i+1] = [200, 0]
                g[0][h+j+1] = [200, 0]
            elif s[i][j] == "T":
                g[i+1][h+w+1] = [200, 0]
                g[h+j+1][h+w+1] = [200, 0]
            elif s[i][j] == "o":
                g[i+1][h+j+1] = [1, 0]
                g[h+j+1][i+1] = [1, 0]

    # 最大流問題を解く関数
    def max_flow(graph_temp, start, end):
        from copy import deepcopy
        graph = deepcopy(graph_temp)

        # DFSを行いpathを算出する部分
        def dfs(start, end, graph):
            path = [start]
            memo = [False]*len(graph)
            memo[start] = True
            while path:
                p = path[-1]
                for b, (c, flow) in graph[p].items():
                    if memo[b] is False and c-flow != 0:
                        memo[b] = True
                        path.append(b)
                        if path[-1] == end:
                            return path
                        break
                else:
                    path.pop(-1)

        # 求めたpathについて水を流す
        def flow_calc(graph, path):
            min_c = 10**20
            for i, p in enumerate(path[1:]):
                min_c = min(min_c, graph[path[i]][p][0]-graph[path[i]][p][1])
            for i, p in enumerate(path[1:]):
                graph[path[i]][p][1] += min_c

        # 残余グラフを作る
        def create_rest(graph):
            l = len(graph)
            rest = [dict() for _ in [0]*l]
            for i, element in list(enumerate(graph)):
                for b, (c, flow) in element.items():
                    if flow != 0:
                        rest[b][i] = [flow, 0]
                    if flow != c:
                        rest[i][b] = [c-flow, 0]
            return rest

        # 残余グラフを反映させる
        def rest_reflect(graph, rest):
            for i, z in enumerate(rest):
                for j, (i0, i1) in z.items():
                    if i1 > 0:
                        if j in graph[i].keys():
                            graph[i][j][1] += i1
                        else:
                            graph[j][i][1] -= i1

        # 計算を行う。一旦通常のグラフ上でDFSを行い、最後に残余グラフで調整する
        while True:
            path = dfs(start, end, graph)
            if path is None:
                break
            else:
                flow_calc(graph, path)
        while True:
            rest = create_rest(graph)
            path = dfs(start, end, rest)
            if path is None:
                break
            flow_calc(rest, path)
            rest_reflect(graph, rest)
        # return graph  # 実際の最大流グラフが知りたい場合はこちらをreturnする
        return sum([j for i, j in graph[0].values()])

    m = max_flow(g, 0, h+w+1)

    if m >= 200:
        print(-1)
    else:
        print(m)


main()

### FIXED CODE ###

def main():
    import sys
    sys.setrecursionlimit(1000000)
    # 行先、上限、逆辺

    def add_edge(From, to, cap):
        g[From][to] = cap
        g[to][-From-1] = 0

    def max_flow(s, t):
        def dfs(v, t, f):
            q.append(v)
            if v == t:
                return f
            used[v] += 1
            for i in range(len(gkey[v])):
                to = gkey[v][i]
                cap = g[v][to]
                if to < 0:
                    to2 = -to-1
                else:
                    to2 = to
                if used[to2] or cap == 0:
                    continue
                d = dfs(to2, t, min(f, cap))
                if d > 0:
                    g[v][to] -= d
                    g[to2][-v-1] += d
                    return d
            q.pop()
            return 0

        flow = 0
        while True:
            used = [0]*n
            q = []
            f = dfs(s, t, 10**100)
            if not q:
                return flow
            flow += f

    h, w = map(int, input().split())
    n = h+w+2
    g = [dict() for _ in range(n)]
    for i in range(h):
        A = input()
        for j in range(w):
            if A[j] == "o":
                add_edge(i, j+h, 1)
                add_edge(j+h, i, 1)
            elif A[j] == "S":
                add_edge(h+w, i, 10**6)
                add_edge(h+w, j+h, 10**6)
            elif A[j] == "T":
                add_edge(i, h+w+1, 10**6)
                add_edge(j+h, h+w+1, 10**6)
    gkey = [list(i.keys()) for i in g]

    ans = max_flow(h+w, h+w+1)
    if ans >= 10**6:
        print(-1)
    else:
        print(ans)


main()