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;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define Alint(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1ll << (n))
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first - r.first, l.second - r.second); }
const lint MOD = 998244353, INF = 1e18;

struct edge { lint to, cap, rev; };

vector<edge> Graph[20000];
int level[20000];
int iter[20000];
void add_edge(lint from, lint to, lint cap) {
	Graph[from].push_back({ to, cap, SZ(Graph[to]) });
	Graph[to].push_back({ from, 0, SZ(Graph[from]) - 1 });
}
void bfs(lint s) {
	memset(level, -1, sizeof(level));
	queue<lint> que;
	que.push(s);
	level[s] = 0;
	while (!que.empty()) {
		lint v = que.front(); que.pop();
		REP(i, SZ(Graph[v])) {
			edge& e = Graph[v][i];
			if (e.cap > 0 && level[e.to] < 0) {
				level[e.to] = level[v] + 1;
				que.push(e.to);
			}
		}
	}
}

lint dfs(lint v, lint t, lint f) {
	if (v == t) return f;
	for (int& i = iter[v]; i < SZ(Graph[v]); i++) {
		edge& e = Graph[v][i];
		if (e.cap > 0 && level[v] < level[e.to]) {
			lint d = dfs(e.to, t, min(f, e.cap));
			if (d > 0) {
				e.cap -= d;
				Graph[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}

lint max_flow(lint s, lint t) {
	lint flow = 0;
	for (;;) {
		bfs(s);
		if (level[t] < 0) return flow;
		memset(iter, 0, sizeof(iter));
		lint f;
		while ((f = dfs(s, t, INF)) > 0) {
			flow += f;
		}
	}
}


lint H, W;
char c;

int main() {
	cin.tie(0); ios_base::sync_with_stdio(false);
	lint N = 500;
	plint st, ed;
	cin >> H >> W;
	REP(i, H) {
		REP(j, W) {
			cin >> c;
			if (c == 'o') {
				add_edge(1 + i, 1 + W + j, 1);
				add_edge(1 + W + j, 1 + i, 1);
			}
			else if (c == 'S') {
				add_edge(0, 1 + i, INF);
				add_edge(0, 1 + W + j, INF);
				st = { i, j };
			}
			else if (c == 'T') {
				add_edge(1 + i, 1 + W + H, INF);
				add_edge(1 + W + j, 1 + W + H, INF);
				ed = { i, j };
			}
		}
	}
	if (st.first == ed.first || st.second == ed.second) {
		cout << -1 << endl;
		return 0;
	}
	lint ans = max_flow(0, 1 + W + H);
	if (ans == INF) cout << -1 << endl;
	else cout << ans << endl;
}