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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

const int MAXM = 1e5 + 10;
const int MAXN = 1e5 + 10;
const int MAXNODE = 100 + 10;
const int INF = 1e9 + 7;

struct Edge {
	int nxt, v, w;
} e[MAXM];
int head[MAXN], tot;

inline void addEdge (int u, int v, int w) {
	e[++tot] = (Edge){head[u],v,w};
	head[u] = tot;
}

void reset (void) {
	memset(head,-1,sizeof(head));
	tot = -1;
}

int n, m, s, t, node_tot;
int cur[MAXN];
int dis[MAXN];
int gap[MAXN];
queue<int> q;

void maxflow_bfs (void) {
	memcpy(cur,head,sizeof(head));
	memset(dis,-1,sizeof(dis));
	memset(gap,0,sizeof(gap));

	gap[0] = 1;
	dis[t] = 0;
	q.push(t);
	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (int i = head[u]; ~i; i = e[i].nxt) {
			if (e[i^1].w > 0 && dis[e[i].v] == -1) {
				dis[e[i].v] = dis[u] + 1;
				gap[dis[e[i].v]]++;
				q.push(e[i].v);
			}
		}
	}
}

int maxflow_dfs (int u, int flow) {
	if (u == t) return flow;

	int sum_flow = 0;
	for (int & i = cur[u]; ~i; i = e[i].nxt) {
		if (e[i].w > 0 && dis[u] == dis[e[i].v] + 1) {
			int new_flow = maxflow_dfs(e[i].v, min(flow, e[i].w));
			e[i].w -= new_flow;
			e[i^1].w += new_flow;
			flow -= new_flow;
			sum_flow += new_flow;
			if (!flow) return sum_flow;
		}
	}
	if (--gap[dis[u]] == 0) dis[s] = node_tot;
	gap[++dis[u]]++;
	cur[u] = head[u];

	return sum_flow;
}

int SAP (void) {
	int ret = 0;
	maxflow_bfs();
	while (dis[s] < node_tot) {
		ret += maxflow_dfs(s, INF);
	}
	return ret;
}

char a[MAXNODE][MAXNODE];

int main (void) {
	reset();
	int h, w;
	scanf("%d%d", &h, &w);

	s = h * w + 1;
	t = h * w + 2;
	node_tot = h * w + 2;

	int a1, a2, b1, b2;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			scanf(" %c", &a[i][j]);
			if (a[i][j] == 'S') {
				a1 = i;
				b1 = j;
				addEdge(s,i*w+j,INF);
				addEdge(i*w+j,s,0);
			} else if (a[i][j] == 'T') {
				a2 = i;
				b2 = j;
				addEdge(i*w+j,t,INF);
				addEdge(t,i*w+j,0);
			}
		}
	}
	if (a1 == a2 || b1 == b2) {
		printf("-1\n");
		return 0;
	}
	for (int i = 0; i < h; i++) {
		int k = -1, ww;
		for (int j = 0; j < w; j++) {
			if (a[i][j] == 'S') continue;
			if (a[i][j] == 'T') continue;
			if (a[i][j] != '.') {
				if (k != -1 || a1 == i || a2 == i) {
					if (a1 == i) {
						k = b1;
						ww = 1;
					} else if (a2 == i) {
						k = b2;
						ww = INF;
					} else ww = 1;
					addEdge(i*w+k,i*w+j,ww);
					addEdge(i*w+j,i*w+k,0);
					addEdge(i*w+j,i*w+k,ww);
					addEdge(i*w+k,i*w+j,0);
				}
				k = j;
			}
		}
	}
	for (int j = 0; j < w; j++) {
		int k = -1, ww;
		for (int i = 0; i < h; i++) {
			if (a[i][j] == 'S') continue;
			if (a[i][j] == 'T') continue;
			if (a[i][j] != '.') {
				if (k != -1 || b1 == j || b2 == j) {
					if (b1 == j) {
						k = a1;
						ww = 1;
					} else if (b2 == j) {
						k = a2;
						ww = INF;
					} else ww = 1;
					addEdge(k*w+j,i*w+j,ww);
					addEdge(i*w+j,k*w+j,0);
					addEdge(i*w+j,k*w+j,ww);
					addEdge(k*w+j,i*w+j,0);
				}
				k = i;
			}
		}
	}
	printf("%d\n", SAP());

}