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 C (GCC 9.2.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>

#define N	100
#define M	100
#define N_	(1 + N + M + 1)
#define M_	(2 + N * M + 2)

int *oh[N_], oo[N_], n_;
int ij[M_], cc[M_ * 2], m_;

void link(int i, int h) {
	int o = oo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		oh[i] = (int *) realloc(oh[i], o * 2 * sizeof *oh[i]);
	oh[i][o] = h;
}

void link_(int i, int j, int c1, int c2) {
	int h = m_++;

	ij[h] = i ^ j, cc[h << 1] = c1, cc[h << 1 | 1] = c2;
	link(i, h << 1), link(j, h << 1 | 1);
}

int dd[N_];

int bfs(int s, int t) {
	static int qu[N_];
	int i, head, cnt;

	for (i = 0; i < n_; i++)
		dd[i] = n_;
	head = cnt = 0;
	dd[s] = 0, qu[head + cnt++] = s;
	while (cnt) {
		int d, o;

		i = qu[cnt--, head++], d = dd[i] + 1;
		for (o = 0; o < oo[i]; o++) {
			int h_ = oh[i][o];

			if (cc[h_]) {
				int h = h_ >> 1, j = i ^ ij[h];

				if (dd[j] > d) {
					dd[j] = d;
					if (j == t)
						return 1;
					qu[head + cnt++] = j;
				}
			}
		}
	}
	return 0;
}

int dfs(int i, int t) {
	int d, o;

	if (i == t)
		return 1;
	d = dd[i] + 1;
	for (o = 0; o < oo[i]; o++) {
		int h_ = oh[i][o];

		if (cc[h_]) {
			int h = h_ >> 1, j = i ^ ij[h];

			if (dd[j] == d && dfs(j, t)) {
				cc[h_]--, cc[h_ ^ 1]++;
				return 1;
			}
		}
	}
	dd[i] = n_;
	return 0;
}

int dinic(int s, int t) {
	int sum;

	sum = 0;
	while (bfs(s, t))
		while (dfs(s, t))
			sum++;
	return sum;
}

int main() {
	static char cc[N][M + 1];
	int n, m, i, j, is, js, it, jt;

	scanf("%d%d", &n, &m), n_ = 1 + n + m + 1;
	for (i = 0; i < n; i++)
		scanf("%s", cc[i]);
	for (i = 0; i < n_; i++)
		oh[i] = (int *) malloc(2 * sizeof *oh[i]);
	is = js = it = jt = 0;
	for (i = 0; i < n; i++)
		for (j = 0; j < m; j++) {
			if (cc[i][j] == '.')
				continue;
			link_(1 + i, 1 + n + j, 1, 1);
			if (cc[i][j] == 'S')
				is = i, js = j;
			else if (cc[i][j] == 'T')
				it = i, jt = j;
		}
	if (is == it || js == jt) {
		printf("-1\n");
		return 0;
	}
	link_(0, 1 + is, n * m + 1, 0);
	link_(0, 1 + n + js, n * m + 1, 0);
	link_(1 + it, n_ - 1, n * m + 1, 0);
	link_(1 + n + jt, n_ - 1, n * m + 1, 0);
	printf("%d\n", dinic(0, n_ - 1));
	return 0;
}