Problem:
Score : 800 points

Problem Statement

There is a pond with a rectangular shape.
The pond is divided into a grid with H rows and W columns of squares.
We will denote the square at the i-th row from the top and j-th column from the left by (i,\ j).

Some of the squares in the pond contains a lotus leaf floating on the water.
On one of those leaves, S, there is a frog trying to get to another leaf T.
The state of square (i,\ j) is given to you by a character a_{ij}, as follows:

. : A square without a leaf.

o : A square with a leaf floating on the water.

S : A square with the leaf S.

T : A square with the leaf T.

The frog will repeatedly perform the following action to get to the leaf T: "jump to a leaf that is in the same row or the same column as the leaf where the frog is currently located."

Snuke is trying to remove some of the leaves, other than S and T, so that the frog cannot get to the leaf T.
Determine whether this objective is achievable. If it is achievable, find the minimum necessary number of leaves to remove.

Constraints

2 ≤ H, W ≤ 100

a_{ij} is ., o, S or T.

There is exactly one S among a_{ij}.

There is exactly one T among a_{ij}.

Input

Input is given from Standard Input in the following format:

H W
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}

Output

If the objective is achievable, print the minimum necessary number of leaves to remove.
Otherwise, print -1 instead.

Sample Input 1

3 3
S.o
.o.
o.T

Sample Output 1

2

Remove the upper-right and lower-left leaves.

Sample Input 2

3 4
S...
.oo.
...T

Sample Output 2

0

Sample Input 3

4 3
.S.
.o.
.o.
.T.

Sample Output 3

-1

Sample Input 4

10 10
.o...o..o.
....o.....
....oo.oo.
..oooo..o.
....oo....
..o..o....
o..o....So
o....T....
....o.....
........oo

Sample Output 4

5

Solution in PyPy3 (7.3.0) (judge verdict: Memory Limit Exceeded)

import sys
input = sys.stdin.readline

class Dinic:
    class Edge:
        def __init__(self, v, cap, rev):
            self.v = v
            self.cap = cap
            self.rev = rev

    def __init__(self, V):
        self.V = V
        self.G = [[] for _ in range(V)]

    def add_edge(self, u, v, cap):
        self.G[u].append(self.Edge(v, cap, len(self.G[v])))
        self.G[v].append(self.Edge(u, 0, len(self.G[u])-1))

    # calculate the shortest distance from s
    def bfs(self, s):
        from collections import deque

        self.level = [-1] * self.V
        self.level[s] = 0
        queue = deque()
        queue.append(s)
        while queue:
            u = queue.popleft()
            for e in self.G[u]:
                if e.cap > 0 and self.level[e.v] == -1:
                    self.level[e.v] = self.level[u] + 1
                    queue.append(e.v)

    # find a path
    def dfs(self, u, t, f):
        if u == t:
            return f
        i = self.iter[u]
        while i < len(self.G[u]):
            e = self.G[u][i]
            if e.cap > 0 and self.level[u] < self.level[e.v]:
                d = self.dfs(e.v, t, min(f, e.cap))
                if d > 0:
                    e.cap -= d
                    self.G[e.v][e.rev].cap += d
                    return d
            i += 1
            self.iter[u] += 1
        return 0

    # find the max flow from s to v
    def max_flow(self, s, t):
        flow = 0
        INF = 10**18
        while True:
            self.bfs(s)
            if self.level[t] == -1:
                return flow
            self.iter = [0] * self.V
            while True:
                f = self.dfs(s, t, INF)
                if f == 0:
                    break
                flow += f

H, W = map(int, input().split())
a = [input().rstrip() for _ in range(H)]
flow = Dinic(2*H*W)
s = t = 0
INF = 10**9
for i in range(H):
    for j in range(W):
        if a[i][j] == '.':
            continue
        v = i * W + j
        flow.add_edge(v, H * W + v, 1)
        if a[i][j] == 'S':
            s = H * W + v
            for k in range(H):
                if a[k][j] == 'o':
                    flow.add_edge(s, k * W + j, INF)
                if a[k][j] == 'T':
                    print(-1)
                    exit()
            for k in range(W):
                if a[i][k] == 'o':
                    flow.add_edge(s, i * W + k, INF)
                if a[i][k] == 'T':
                    print(-1)
                    exit()
        if a[i][j] == 'T':
            t = v
        if a[i][j] == 'o':
            for k in range(H):
                if a[k][j] in 'oT':
                    flow.add_edge(H * W + v, k * W + j, INF)
            for k in range(W):
                if a[i][k] in 'oT':
                    flow.add_edge(H * W + v, i * W + k, INF)
print(flow.max_flow(s, t))