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: Accepted)

#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#define ll long long
#define lowbit(x) x&-x
using namespace std;
int read(){
    char c;int s=0,t=1;
    while(!isdigit(c=getchar()))if(c=='-')t=-1;
    do{s=s*10+c-'0';}while(isdigit(c=getchar()));
    return s*t;
}
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a<b?b:a;}
int ab(int x){return x>0?x:-x;}
//int MO(int x){return x>=MOD?x-MOD:x;}
//void insert(int u,int v){tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;}
/*------------------------------------------------------------*/
const int inf=0x3f3f3f3f,p=310,maxn=200010,maxm=600010;

int n,idx[p][p],idy[p][p],m,map[p][p],S,T;
namespace nwf{
	int first[maxn],cur[maxn],tot=1,q[maxn],d[maxn];
	struct edge{int v,from,f;}e[maxm*2];
	void insert(int u,int v,int f){
		//printf("%d %d %d\n",u,v,f);
		tot++;e[tot].v=v;e[tot].f=f;e[tot].from=first[u];first[u]=tot;
		tot++;e[tot].v=u;e[tot].f=0;e[tot].from=first[v];first[v]=tot;
	}
	bool bfs(){
		memset(d,-1,sizeof(d));
		int head=0,tail=1;q[head]=S;d[S]=0;
		while(head<tail){
			int x=q[head++];
			for(int i=first[x];i;i=e[i].from)if(e[i].f&&d[e[i].v]==-1){
				d[e[i].v]=d[x]+1;
				q[tail++]=e[i].v;
			}
		}
		return d[T]!=-1;
	}
	int dfs(int x,int a){
		if(x==T||a==0)return a;
		int flow=0,f;
		for(int& i=cur[x];i;i=e[i].from)if(e[i].f&&d[e[i].v]==d[x]+1&&(f=dfs(e[i].v,min(a,e[i].f)))){
			e[i].f-=f;e[i^1].f+=f;
			a-=f;flow+=f;
			if(a==0)break;
		}
		return flow;
	}
	int dinic(){
		int ans=0;
		while(bfs()){
			for(int i=S;i<=T;i++)cur[i]=first[i];
			ans+=dfs(S,inf);
		}
		return ans;
	}
}
char s[p];
int main(){
//	freopen("trade4.in","r",stdin);
//	freopen("trade.out","w",stdout);
    n=read();m=read();
    int cnt=0;S=0;T=n*m*2+1;
    for(int i=1;i<=n;i++){
    	scanf("%s",s+1);
    	for(int j=1;j<=m;j++){
    		idx[i][j]=++cnt;idy[i][j]=++cnt;
    		if(s[j]!='.'){
    			map[i][j]=1;//printf("i=%d j=%d\n",i,j);
    			if(s[j]=='S')nwf::insert(S,idy[i][j],inf),nwf::insert(S,idx[i][j],inf);
    			if(s[j]=='T')nwf::insert(idx[i][j],T,inf),nwf::insert(idy[i][j],T,inf);
    		}
    	}
    }
    for(int i=1;i<=n;i++){
    	for(int j=1;j<=m;j++){
    		if(map[i][j])nwf::insert(idx[i][j],idy[i][j],1),nwf::insert(idy[i][j],idx[i][j],1);
    		if(j<m)nwf::insert(idx[i][j],idx[i][j+1],inf),nwf::insert(idx[i][j+1],idx[i][j],inf);
    		if(i<n)nwf::insert(idy[i][j],idy[i+1][j],inf),nwf::insert(idy[i+1][j],idy[i][j],inf);
    	}
    }
    int ans=nwf::dinic();
    if(ans>=inf)printf("-1");else printf("%d",ans);
    return 0;
}