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 <bits/stdc++.h>

using namespace std;
string S[101];
int s , t ;
int dx[]={1 , 0  , -1 , 0 };
int dy[]={0  , 1 , 0 , -1 };
int g[100][100];

const int N = 200024, E = 20000000;    ///N must be a power of 2
int nbr;
int head[N],to[E],nxt[E],cap[E];
int e;
void init()
{
    e=0;
    memset(head,-1,sizeof head);
}
void addEdge(int f, int t, int w)
{
    nxt[e]=head[f];
    head[f]=e;
    cap[e]=w;
    to[e++]=t;
}
void addBiEdge(int f,int t,int w)
{
    addEdge(f,t,w);
    addEdge(t,f,0);
}
pair<int,int> mp[10001];

typedef int LL;
int vis[N],ID;
int fromid[N];
int fromnode[N];
LL bfs(int src,int snk) {

    queue<int> q ;
    q.push(src);
    vis[src]=ID;
    while(!q.empty()){
        int cur=q.front();
        q.pop();
        if(cur==snk){
            while(cur!=src){
                cap[fromid[cur]]-=1;
                cap[fromid[cur]^1]+=1;
                cur=fromnode[cur];
            }
            return 1;
        }
        for (int i = head[cur]; i != -1; i = nxt[i]) {
            int t = to[i];
            if(cap[i]>0 &&vis[t]!=ID){
                fromid[t]=i;
                fromnode[t]=cur;
                vis[t]=ID;
                q.push(t);
            }
        }
    }
    return 0;
}
// +201278474570 tamer :)

int out(int i){
    return i*2+1;
}

int in(int i){
    return i*2;
}

int maxFlow(int src,int snk) {
    int ret = 0;
    if (src == snk)
        return INT_MAX;
    int f;
    for (ID++; (f = bfs(out(src),in(snk))), f; ID++)
        ret += f ;
    return ret;
}
int n , m ;

bool get(int x , int y){
    return (x>=0 && x<n && y>=0 &&y<m);
}

int main()
{

    cin >> n >> m ;
    for(int i = 0 ; i< n ; i++){
        cin >> S[i];
//
//        for(int j = 0 ; j<m ; j++){
//            if(i==0 &&j==0)S[i]+='S';
//            else if(i==n-1&&j==m-1)S[i]+='T';
//            else S[i]+='o';
//        }
    }
    int nbr = 0;
    for(int i = 0 ; i< n ; i++){
        for(int j = 0 ; j< m ; j++){
            if(S[i][j]=='.')continue;
            g[i][j]= ++nbr;
            mp[nbr]=make_pair(i,j);
            if(S[i][j]=='S')s=nbr;
            if(S[i][j]=='T')t=nbr;
        }
    }
    init();
    bool ANS=1;
    for(int i = 1 ; i<= nbr ; i++){
        int x = mp[i].first ;
        int y = mp[i].second ;
        addBiEdge(in(i),out(i),1);
//        cout << x << ' ' << y <<endl;
        for(int k = 0 ; k< 4 ; k++){
            int xx= x ;
            int yy= y ;
            while(1){
                xx+= dx[k];
                yy+= dy[k];
//                cout << xx << ' ' << yy <<endl;
                if(!get(xx,yy))break;
                if(g[xx][yy]){
                    addBiEdge(out(i),in(g[xx][yy]),1<<10);
                    if(i==s && g[xx][yy]==t)ANS =0 ;
                    if(i==t && g[xx][yy]==s)ANS =0 ;
                }
            }
        }
    }
    if(!ANS)cout<<-1 ;
    else
    cout<<maxFlow(s,t);
    return 0;
}