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;
const long long llINF = 9223372036854775807;
//const int INF = 2147483647;
const int INF=1e9;
const int maxn = 2e5 + 7;
const int maxm = 1e6 + 7;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-10;

struct Edge
{
    int from, to, cap, flow;
};

int n, m, s, t, num;
vector<Edge> edges;    // 边数的两倍
vector<int> G[maxn];   // 邻接表，G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[maxn];        // BFS使用
int d[maxn];           // 从起点到i的距离
int cur[maxn];         // 当前弧指针

void ClearAll(int n)
{
    for (int i = 0; i < n; i++)
        G[i].clear();
    edges.clear();
}

void ClearFlow()
{
    for (int i = 0; i < edges.size(); i++)
        edges[i].flow = 0;
}

void AddEdge(int from, int to, int cap)
{
    edges.push_back((Edge) {from, to, cap, 0});
    edges.push_back((Edge) {to, from, 0, 0});
    num = edges.size();
    G[from].push_back(num - 2);
    G[to].push_back(num - 1);
}

bool BFS()
{
    memset(vis, 0, sizeof(vis));
    queue<int> Q;
    Q.push(s);
    vis[s] = 1;
    d[s] = 0;
    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)
{
    int flow = 0;
    while (BFS())
    {
        memset(cur, 0, sizeof(cur));
        flow += DFS(s, INF);
    }
    return flow;
}

int h, w;
char pic[107][107];

int main(int argc, char const *argv[])
{
    scanf("%d%d", &h, &w);
    s = 0;
    t = h + w + 1;
    for (int i = 1; i <= h; i++)
        scanf("%s", pic[i] + 1);

    for (int i = 1; i <= h; i++)
    {
        for (int j = 1; j <= w; j++)
        {
            //char c;
            //cin>>c;

            if (pic[i][j] != '.')
            {
                AddEdge(i, j + h, 1);
                AddEdge(j + h, i, 1);
            }
            if (pic[i][j] == 'S')
            {
                AddEdge(s, i, INF);
                AddEdge(s, j + h, INF);
            }
            if (pic[i][j] == 'T')
            {
                AddEdge(i, t, INF);
                AddEdge(j + h, t, INF);
            }

        }
    }

    int ans = Maxflow(s, t);
    if (ans >= INF)
        printf("-1\n");
    else
        printf("%d\n", ans);
    return 0;
}