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 <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;

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

int V;
vector<edge> G[1000000];
ll level[1000000];
int iter[1000000];

void addedge(int from, int to, ll cap) {
    G[from].push_back((edge){to, cap, (ll)G[to].size()});
    G[to].push_back((edge){from, 0, (ll)G[from].size() - 1});
}

void dinicbfs(int s) {
    fill(level, level + V, -1);
    queue<ll> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
        ll v = que.front();
        que.pop();
        for (int i = 0; i < (int)G[v].size(); i++) {
            edge e = G[v][i];
            if (e.cap > 0 && level[e.to] == -1) {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}

ll dinicdfs(ll v, ll t, ll f) {
    if (v == t) {
        return f;
    }
    for (int &i = iter[v]; i < (int)G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && level[v] < level[e.to]) {
            ll d = dinicdfs(e.to, t, min(f, e.cap));
            if (d > 0) {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

ll dinic(int s, int t) {
    ll flow = 0;
    while (1) {
        dinicbfs(s);
        if (level[t] < 0) {
            return flow;
        }
        fill(iter, iter + V, 0);
        ll f;
        while ((f = dinicdfs(s, t, inf)) > 0) {
            flow += f;
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll H, W;
    cin >> H >> W;
    V = H + W + 2;
    plglg st, ed;
    REP(i, 0, H) {
        string s;
        cin >> s;
        REP(j, 0, W) {
            if (s[j] != '.') {
                if (s[j] == 'S') {
                    st = plglg(i, j);
                    addedge(H + W, i, inf);
                    addedge(i, H + W, inf);
                    addedge(H + W, H + j, inf);
                    addedge(H + j, H + W, inf);
                } else if (s[j] == 'T') {
                    ed = plglg(i, j);
                    addedge(H + W + 1, i, inf);
                    addedge(H + W + 1, H + j, inf);
                    addedge(i, H + W + 1, inf);
                    addedge(H + j, H + W + 1, inf);
                } else {
                    addedge(i, H + j, 1);
                    addedge(H + j, i, 1);
                }
            }
        }
    }
    if (st.first == ed.first || st.second == ed.second) {
        cout << -1 << endl;
        return 0;
    }
    // REP(i, 0, H + W + 2) {
    //     REP(j, 0, G[i].size()) {
    //         edge e = G[i][j];
    //         cout << e.to << " " << e.cap << " aaa ";
    //     }
    //     cout << endl;
    // }
    ll ans = dinic(H + W, H + W + 1);
    cout << ans << endl;
}