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 "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)

#define MAX_V (10000*2)

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

vector<edge> G[MAX_V];  //グラフのパス
int level[MAX_V];       //始点からの距離
int iter[MAX_V];        //どこまで調べたか

// fromからtoへ向かう、容量capの辺をグラフに追加する
void add_edge(int from,int to,int cap){
    G[from].push_back(  (edge){ to  ,cap,(int)G[to  ].size()   }  );
    G[to  ].push_back(  (edge){ from,0  ,(int)G[from].size()-1 }  );
}

// sからの最短距離をBFSで計算する
void bfs(int s){
    rep(i,MAX_V)level[i]=-1;
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v=que.front();que.pop();
        
        rep(i,G[v].size()){
            edge &e=G[v][i];
            if(e.cap>0 && level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}

//増加パスをDFSで探す
int dfs(int v,int t,int f){
    if(v==t)return f;
    for(int &i=iter[v];i<G[v].size();i++){
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to]){
            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;
}

// sからtへの最大流を求める
int max_flow(int s,int t){
    if(s==t)return INF;
    int flow=0;
    for(;;){
        bfs(s);
        if(level[t]<0)return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,INF))>0){
            flow+=f;
        }
    }
}

int h,w;

int f(ll y,ll x){
    return y*w+x;
}

int main(){
    cin>>h>>w;
    vector<string> vs(h);
    rep(i,h)cin>>vs[i];
    
    pii s,t;
    rep(i,h)rep(j,w){
        if(vs[i][j]=='S')s = pii(i,j);
        if(vs[i][j]=='T')t = pii(i,j);
    }
    
    rep(i,h){
        rep(j,w){
            if(vs[i][j]=='o')
                add_edge(f(i,j)+h*w,f(i,j),1);
        }
    }
    rep(i,h){
        rep(j,w){
            if(vs[i][j]=='.')continue;
            for(ll k=i+1;k<h;k++){
               if( vs[k][j]!='.' ){
                    add_edge(f(i,j),f(k,j)+h*w,INF);
                    add_edge(f(k,j),f(i,j)+h*w,INF);
                }
            }
            for(ll l=j+1;l<w;l++){
                if( vs[i][l]!='.' ){
                    add_edge(f(i,j),f(i,l)+h*w,INF);
                    add_edge(f(i,l),f(i,j)+h*w,INF);
                }
            }
        }
    }
    int res = max_flow(f(s.first,s.second), f(t.first,t.second)+h*w);
    if(res>=INF)cout<<-1<<endl;
    else cout<<res<<endl;
}