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++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
#define ll long long
#define mk make_pair
using namespace std;
 
const int N = 3e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9;

int ID, id[105][105];
char s[105][105];
pair<int, int> ss, tt;

vector<int> Eid[N];
int n, m, cnt, S, T, in[N], out[N], cur[N], d[N];
struct Edge {
	int c, v;
} E[N];
void AddEdge(int x, int y, int c) {
	E[cnt].v = y, E[cnt].c = c;
	Eid[x].push_back(cnt++);
	E[cnt].v = x, E[cnt].c = 0;
	Eid[y].push_back(cnt++);
}
bool BFS() {
	static int Q[N];
	int x, ql, qr;
	for (int i = 0; i <= T; i++) d[i] = -1;
	Q[ql = qr = 0] = S; d[S] = 0;
	while (ql <= qr) {
		x = Q[ql++];
		for (int i = 0; i < Eid[x].size(); i++) {
			const Edge &e = E[Eid[x][i]];
			if (e.c && d[e.v] == -1) {
				Q[++qr] = e.v;
				d[e.v] = d[x] + 1;
				if (e.v == T) return 1;
			}
		}
	}
	return 0;
}
int Aug(int x, int a) {
	if (x == T) return a;
	int aug = 0, delta;
	for (int &i = cur[x]; i < Eid[x].size(); i++) {
		Edge &e = E[Eid[x][i]];
		if (e.c && d[e.v] == d[x] + 1) {
			delta = Aug(e.v, min(a, e.c));
			if (delta) {
				e.c -= delta;
				E[Eid[x][i] ^ 1].c += delta;
				aug += delta;
				if (!(a -= delta)) break;
			}
		}
	}
	if (!aug) d[x] = -1;
	return aug;
}
int MaxFlow() {
	int flow = 0;
	while(BFS()) {
		for (int i = 0; i <= T; i++) cur[i] = 0;
		flow += Aug(S, inf);	
	}
	return flow;
}

int main() {
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%s", s[i] + 1);
		for (int j = 1; j <= m; j++) {
			if (s[i][j] == 'S') ss = mk(i, j);
			if (s[i][j] == 'T') tt = mk(i, j);
			if (s[i][j] == 'o') id[i][j] = ++ID;
		}
	}
	if (ss.first == tt.first) return puts("-1");
	if (ss.second == tt.second) return puts("-1");
	T = 2 * ID + 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (s[i][j] != 'o') continue;
			AddEdge(id[i][j], id[i][j] + ID, 1);
			for (int k = 1; k <= m; k++) {
				if (k == j) continue;
				if (s[i][k] == 'o') {
					AddEdge(id[i][j] + ID, id[i][k], inf);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (s[i][j] != 'o') continue;
			//AddEdge(id[i][j], id[i][j] + ID, 1);
			for (int k = 1; k <= n; k++) {
				if (k == i) continue;
				if (s[k][j] == 'o') {
					AddEdge(id[i][j] + ID, id[k][j], inf);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (s[i][j] != 'S') continue;
		//	AddEdge(id[i][j], id[i][j] + ID, 1);
			for (int k = 1; k <= m; k++) {
				if (k == j) continue;
				if (s[i][k] == 'o') {
					AddEdge(S, id[i][k], inf);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (s[i][j] != 'S') continue;
			//AddEdge(id[i][j], id[i][j] + ID, 1);
			for (int k = 1; k <= n; k++) {
				if (k == i) continue;
				if (s[k][j] == 'o') {
					AddEdge(S, id[k][j], inf);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (s[i][j] != 'T') continue;
		//	AddEdge(id[i][j], id[i][j] + ID, 1);
			for (int k = 1; k <= m; k++) {
				if (k == j) continue;
				if (s[i][k] == 'o') {
					AddEdge(id[i][k] + ID, T, inf);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (s[i][j] != 'T') continue;
			//AddEdge(id[i][j], id[i][j] + ID, 1);
			for (int k = 1; k <= n; k++) {
				if (k == i) continue;
				if (s[k][j] == 'o') {
					AddEdge(id[k][j] + ID, T, inf);
				}
			}
		}
	}
	cout << MaxFlow();
}