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;

struct Network
{
    int N = 2, source = 0, sink = 1;
    struct Edge { int source, target, id, reverse; };
    vector<vector<Edge>> adjacency_lists;
    vector<int> capacities, flows;

    Network() : adjacency_lists(2) {}

    int add_vertex()
    {
        adjacency_lists.push_back({});
        return N++;
    }

    void add_undirected_edge(const int a, const int b, const int capacity)
    {
        add_edge(a, b, capacity);
        add_edge(b, a, capacity);
    }

    void add_edge(const int source, const int target, const int capacity)
    {
        if (source == target) return;
        const auto id = int(capacities.size());
        const auto reverse = id+1;
        adjacency_lists[source].push_back({source, target, id, reverse});
        adjacency_lists[target].push_back({target, source, reverse, id});
        capacities.insert(end(capacities), {capacity, 0});
        flows.insert(end(flows), {0, 0});
    }

    int maxflow()
    {
        auto seen = vector<bool>(N);
        const function<int(int, int)> dfs = [&](const int v, const int flow)
        {
            if (v == sink) return flow;

            if (seen[v]) return 0;
            else seen[v] = true;

            for (const auto e : adjacency_lists[v])
            {
                const auto residual = capacities[e.id] - flows[e.id];
                if (residual == 0) continue;
                if (const auto x = dfs(e.target, min(flow, residual)))
                {
                    flows[e.id] += x;
                    flows[e.reverse] -= x;
                    return x;
                }
            }
            return 0;
        };

        auto flow = 0;
        while (true)
        {
            fill(begin(seen), end(seen), false);
            const auto delta = dfs(source, numeric_limits<int>::max());
            if (delta > 0) flow += delta;
            else break;
        }
        return flow;
    }
};

int main()
{
    int R, C; cin >> R >> C;

    auto network = Network();

    auto rows = vector<int>(R);
    for (auto r = 0; r < R; r++)
        rows[r] = network.add_vertex();

    auto columns = vector<int>(C);
    for (auto c = 0; c < C; c++)
        columns[c] = network.add_vertex();

    const auto infinity = R + C;
    for (auto r = 0; r < R; r++)
    {
        string row; cin >> row;
        for (auto c = 0; c < C; c++)
        {
            if (row[c] == 'o')
                network.add_undirected_edge(rows[r], columns[c], 1);
            else if (row[c] == 'S')
            {
                network.source = rows[r];
                network.add_undirected_edge(rows[r], columns[c], infinity);
            }
            else if (row[c] == 'T')
            {
                network.sink = columns[c];
                network.add_undirected_edge(rows[r], columns[c], infinity);
            }
        }
    }

    const auto mincut = network.maxflow();
    cout << (mincut >= infinity ? -1 : mincut) << endl;
}