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<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 10010
#define M 110
#define Inf 2147483647
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
struct note{
	int to,next,flow;
};note edge[N*10];
int i,j,n,m,ans,x,S,T,la;
int sx,sy,tx,ty;
int tot,head[N];
int height[N],qu[N*100];
int map[M][M],pos[M][M];
char ch;
void lb(int x,int y,int z){
	edge[++tot].to=y;edge[tot].next=head[x];edge[tot].flow=z;head[x]=tot;
	edge[++tot].to=x;edge[tot].next=head[y];edge[tot].flow=0;head[y]=tot;
}
bool BFS(){
	int i,x,l=0,r=1;
	memset(height,-1,sizeof(height));
	height[S]=0;
	qu[1]=S;
	while(l<r){
		x=qu[++l];
		for(i=head[x];i;i=edge[i].next)
		    if(edge[i].flow>0&&height[edge[i].to]==-1){
		    	height[edge[i].to]=height[x]+1;
		    	qu[++r]=edge[i].to;
			}
	}
	return ~height[T]?1:0;
}
int Min(int x,int y){return x<y?x:y;}
int find(int u,int thin){
	if(u==T)return thin;
	int i,a,sum=0;
	for(i=head[u];i;i=edge[i].next)
	    if(edge[i].flow>0&&height[edge[i].to]==height[u]+1){
	    	a=find(edge[i].to,Min(thin,edge[i].flow));
	    	if(a>0){
	    		thin-=a;
	    		sum+=a;
	    		edge[i].flow-=a;
	    		edge[i^1].flow+=a;
	    		if(!thin)return sum;
			}
		}
	if(!sum)height[u]=-1;
	return sum;
}
void Dinic(){
	while(BFS())ans+=find(S,Inf);
}
int main(){
    scanf("%d%d\n",&n,&m);
    tot=1;
    fo(i,1,n){
    	fo(j,1,m){
    		pos[i][j]=++x;
    		ch=getchar();
    		if(ch=='o')map[i][j]=1;
    		if(ch=='S')sx=i,sy=j;
    		if(ch=='T')tx=i,ty=j;
		}
    	ch=getchar();
	}
	if(sx==tx||sy==ty){printf("-1");return 0;}
	fo(i,1,m)swap(map[sx][i],map[1][i]);sx=1;
	fo(i,1,n)swap(map[i][sy],map[i][1]);sy=1;
	fo(i,1,m)swap(map[tx][i],map[n][i]);tx=n;
	fo(i,1,n)swap(map[i][ty],map[i][m]);ty=m;
	S=1,T=n*m;
	fo(i,2,m)if(map[1][i])lb(S,pos[1][i],1);
	fo(i,2,n)if(map[i][1])lb(S,pos[i][1],1);
	fo(i,1,m-1)if(map[n][i])lb(pos[n][i],T,1);
	fo(i,1,n-1)if(map[i][m])lb(pos[i][m],T,1);
	fo(i,1,m){
		la=0;
		fo(j,1,n)if(map[j][i]){
			if(!la)la=j;else
			lb(pos[la][i],pos[j][i],Inf),la=j;
		}
	}
	fo(i,1,n){
		la=0;
		fo(j,1,m)if(map[i][j]){
			if(!la)la=j;else
			lb(pos[i][la],pos[i][j],Inf),la=j;
		}
	}
	Dinic();
	printf("%d",ans);
	return 0;
}