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 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<stdio.h>
int MIN(int a,int b){return a<b?a:b;}
int ta[1010],to[1000010],nt[1000010],co[1000010],F[1010];
int MF(int s,int t,int e,int min){
  if(s==t||min==0)return min;
  int i,r;
  F[s]=1;
  for(i=ta[s];i+1;i=nt[i]){
    if(F[to[i]])continue;
    r=MF(to[i],t,e,MIN(min,co[i]));
    co[i]-=r;
    co[(i+e)%(2*e)]+=r;
    if(r)return r;
  }
  return 0;
}
int maxf(int s,int t,int v,int e,int *a,int *b,int *c){
  int i,f,r;
  for(i=0;i<v;i++)ta[i]=-1;
  for(i=0;i<e;i++){
    nt[i  ]=ta[to[i+e]=a[i]];
    nt[i+e]=ta[to[i  ]=b[i]];
    co[ta[a[i]]=i  ]=c[i];
    co[ta[b[i]]=i+e]=0;
  }
  for(f=0;r=MF(s,t,e,1000000000);f+=r){
    for(i=0;i<v;i++)F[i]=0;
    if(f>1e9)return f;
  }
  return f;
}
int main(){
  int h,w,s,g,r=0,a[100010],b[100010],c[100010],e=0,ans,i,j,k,d[110][110];
  char p;
  scanf("%d %d",&h,&w);
  for(i=0;i<h;i++){
    for(j=0;j<w;j++){
      d[i][j]=-1;
      scanf(" %c",&p);
      if(p=='S')s=r;
      if(p=='T')g=r;
      if(p-'.')d[i][j]=r++;
    }
  }
  for(i=0;i<h;i++){
    for(j=0;j<w;j++){
      if(d[i][j]<0)continue;
      a[e  ]=d[i][j]+r;
      b[e  ]=d[i][j];
      c[e++]=d[i][j]==s||d[i][j]==g?1e9:1;
      for(k=0;k<w;k++){
	if(d[k][j]<0||i==k)continue;
	a[e  ]=d[i][j];
	b[e  ]=d[k][j]+r;
	c[e++]=1e9;
      }
      for(k=0;k<w;k++){
	if(d[i][k]<0||k==j)continue;
	a[e  ]=d[i][j];
	b[e  ]=d[i][k]+r;
	c[e++]=1e9;
      }
    }
  }
  /*for(i=0;i<h;i++){
    for(j=0;j<w;j++)printf("%3d ",d[i][j]);printf("\n");
    }printf("\n");
    for(i=0;i<e;i++)printf("%d %d %d\n",a[i],b[i],c[i]);//*/
  ans=maxf(s,g,r*2,e,a,b,c);
  printf("%d\n",ans<1e9?ans:-1);
  return 0;
}