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 mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
#define rg register
using namespace std;
typedef long long LL;
const int N = 205;
const int M = 20019;
const int S = 201, T = 202;
const int INF = 0x3f3f3f3f;

template <typename T> inline void read(T &AKNOI) {
	T x = 0, flag = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') flag = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	AKNOI = flag * x;
}

char g[N];
int n, m;
int level[N], q[N], head, tail;

struct Edge {
	int v, w, nxt;
}e[M];
int first[N], used[N], eCnt;

inline void AddEdge(int u, int v, int w) {
	e[++eCnt].v = v;
	e[eCnt].w = w;
	e[eCnt].nxt = first[u];
	first[u] = eCnt;
}

inline void Add(int u, int v, int w) {
	AddEdge(u, v, w);
	AddEdge(v, u, 0);
}

bool Layer() {
	for (int i=1;i<=T;++i) {
		level[i] = -1;
		used[i] = first[i];
	}
	level[S] = 0;
	q[head=tail=1] = S;
	while (head <= tail) {
		int u = q[head++];
		if (u == T) return 1;
		for (int i=first[u];i;i=e[i].nxt) {
			int v = e[i].v;
			if (e[i].w > 0 && level[v] == -1) {
				level[v] = level[u] + 1;
				q[++tail] = v;
			}
		}
	}
	return 0;
}

int DFS(int u, int flow) {
	if (u == T) return flow;
	int ret = 0;
	for (int i=used[u];i;i=e[i].nxt, used[u]=i) {
		int v = e[i].v;
		if (e[i].w > 0 && level[v] == level[u] + 1) {
			int tmp = DFS(v, min(flow, e[i].w));
			e[i].w -= tmp;
			e[i^1].w += tmp;
			flow -= tmp;
			ret += tmp;
			if (!flow) break;
		}
	}
	return ret;
}

void init() {
	read(n); read(m);
	eCnt = 1;
	for (int i=1;i<=n;++i) {
		scanf("%s", g + 1);
		for (int j=1;j<=m;++j) {
			if (g[j] == '.') continue;
			Add(i, n+j, 1);
			Add(n+j, i, 1);
			if (g[j] == 'S') {
				Add(S, i, INF);
				Add(S, n+j, INF);
			} else if (g[j] == 'T') {
				Add(i, T, INF);
				Add(n+j, T, INF);
			}
		}
	}
}

void solve() {
	int ans = 0;
	while (Layer()) {
		ans += DFS(S, INF);
	}
	printf("%d\n", ans);
}

int main() {
	init();
	solve();
	return 0;
}