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 <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e5+50;
const int mod = 1e9+7;
struct Edge{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
    int s,t;
    vector<Edge>edges;
    vector<int> G[N];
    bool vis[N];
    int d[N];
    int cur[N];
    void init(){
       for (int i=0;i<=t+1;i++)
           G[i].clear();
       edges.clear();
    }
    void AddEdge(int from,int to,int cap){
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        int mm=edges.size();
        G[from].push_back(mm-2);
        G[to].push_back(mm-1);
    }
    bool BFS(){
        memset(vis,0,sizeof(vis));
        queue<int>q;
        q.push(s);
        d[s]=0;
        vis[s]=1;
        while (!q.empty()){
            int x = q.front();q.pop();
            for (int i = 0;i<G[x].size();i++){
                Edge &e = edges[G[x][i]];
                if (!vis[e.to] && e.cap > e.flow){
                    vis[e.to]=1;
                    d[e.to] = d[x]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }
  
    int DFS(int x,int a){
        if (x==t || a==0)
            return a;
        int flow = 0,f;
        for(int &i=cur[x];i<G[x].size();i++){
            Edge &e = edges[G[x][i]];
            if (d[x]+1 == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0){
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if (a==0)
                    break;
            }
        }
        return flow;
    }
  
    int Maxflow(int s,int t){
        this->s=s;
        this->t=t;
        int flow = 0;
        while (BFS()){
            memset(cur,0,sizeof(cur));
            flow+=DFS(s,inf);
        }
        return flow;
    }
}dc;
char s[150][150];
int main(){
    int h,w;
    scanf("%d%d", &h,&w);
    int sx,sy,tx,ty;
    for (int i = 1; i <= h; ++i)
    {
        scanf("%s", s[i]+1);
        for (int j = 1; s[i][j] ; ++j)
        {
            if(s[i][j]=='S') sx=i,sy=j;
            else if(s[i][j]=='T') tx=i,ty=j;
        }
    }
    if(sx==tx||sy==ty){
        printf("-1\n");
        return 0;
    }
 
    int tot=w*h;
    int S=tot+1,T=tot+2;
    dc.init();
    for (int i = 1; i <= h; ++i)
    {
        if(s[i][sy]=='o') dc.AddEdge(S,i*w+sy,1); 
        if(s[i][ty]=='o') dc.AddEdge(i*w+ty+tot,T,1);
    }
    for (int j = 1; j <= w; ++j)
    {
        if(s[sx][j]=='o') dc.AddEdge(S,sx*w+j,1);
        if(s[tx][j]=='o') dc.AddEdge(tx*w+j+tot,T,1);
    }
    for (int i = 1; i <= h; ++i)
    {
        for (int j = 1; j <= w; ++j)
        {
            if(s[i][j]!='o') continue;
            dc.AddEdge(i*w+j,i*w+j+tot,1);
            for (int k = 1; k <= h; ++k)
            {
                if(k!=i&&s[k][j]=='o') dc.AddEdge(k*w+j+tot,i*w+j,1);
            }
            for (int k = 1; k <= w; ++k)
            {
                if(k!=j&&s[i][k]=='o') dc.AddEdge(i*w+k+tot,i*w+j,1);
            }
        }
    }
    printf("%d\n", dc.Maxflow(S,T));
    return 0;
}
/* data

3 3
So.
ooo
.oT

1

*/