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: Time Limit Exceeded)

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>
 
#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
 
using namespace std;
 
#define int long long
//typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      P;
 
const int INF=1e+9;
const int INF2 = 1e+18;
const double EPS=1e-9;
const int MOD=1000000007;
 
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

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

int ls = 2;
vector<edge> G[20000];
bool used[20000];

void add_edge(int from,int to,int cap){
	G[from].push_back({to,cap,G[to].size()});
	G[to].push_back({from,0,G[from].size() - 1});
}

int dfs(int v,int t,int f){
	if(v == t) return f;
	used[v] = true;
	for(int i = 0;i < G[v].size();i++){
		edge &e = G[v][i];
		if(!used[e.to] && e.cap > 0){
			int d = dfs(e.to,t,min(f,e.cap));
			if(d > 0){
				e.cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}

int max_flow(){
	int flow = 0;
	while(1){
		memset(used,0,sizeof(used));
		int f = dfs(ls,1,INF);
		if(f == 0) return flow;
		flow += f;
	}
}

signed main(){
	int h,w,sx,sy,tx,ty;
	string field[300];
	int num[300][300];
	cin >> h >> w;
	for(int i = 0;i < h;i++){
		for(int j = 0;j < w;j++) num[i][j] = -1;
	}
	for(int i = 0;i < h;i++) {
		cin >> field[i];
		for(int j = 0;j < w;j++){
			if(field[i][j] == 'S'){
				sx = i;
				sy = j;
				num[i][j] = 0;
			}else if(field[i][j] == 'T'){
				tx = i;
				ty = j;
				num[i][j] = 1;
			}
			else if(field[i][j] == 'o') num[i][j] = ls++;
		}
	}
	if(sx == tx || sy == ty){
		cout << -1 << endl;
		return 0;
	}
	for(int i = 0;i < h;i++){
		for(int j = 0;j < w;j++){
			if(num[i][j] != -1){
				for(int k = 0;k < h;k++){
					if(num[k][j] != -1) add_edge(num[i][j] + ls,num[k][j],5000);
				}
				for(int k = 0;k < w;k++){
					if(num[i][k] != -1) add_edge(num[i][j] + ls,num[i][k],5000);
				}
				if(num[i][j] >= 2) add_edge(num[i][j],num[i][j] + ls,1);
			}
		}
	}
	cout << max_flow() << endl;
	return 0;
}