#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
ll INF = 1e9;
struct edge { int to, cap, rev; };
int MAXV = 21000;
vector<vector<edge>> g(MAXV);
vector<int> level(MAXV);
vector<int> iter(MAXV);
void add_edge(int from, int to, int cap)
{
    g[from].push_back((edge){to, cap, g[to].size()});
    g[to].push_back((edge){from, 0, g[from].size() - 1});
}
void bfs(int s)
{
    level.assign(MAXV, -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while(!que.empty())
    {
        int v = que.front();
        que.pop();
        for(edge &e : g[v])
        {
            if(e.cap > 0 && level[e.to] < 0)
            {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}
int dfs(int v, int t, int f)
{
    if(v == t) return f;
    for(int &i = iter[v]; i < g[v].size(); i++)
    {
        edge &e = g[v][i];
        if(e.cap > 0 && level[v] < level[e.to])
        {
            int d = dfs(e.to, t, min(f, e.cap));
            if(d > 0)
            {
                e.cap -= d;
                g[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow(int s, int t)
{
    int flow = 0;
    while(true)
    {
        bfs(s);
        if(level[t] < 0) return flow;
        iter.assign(MAXV, 0);
        int f = 0;
        while((f = dfs(s, t, INF)) > 0) flow += f;
    }
}
int main()
{
    int H, W;
    cin >> H >> W;
    map<P, int> ma;
    int source = 0;
    int sink = 1;
    int id = 2;
    int sx, sy, tx, ty;
    for(int i = 0; i < H; i++)
    {
        for(int j = 0; j < W; j++)
        {
            char c;
            cin >> c;
            if(c == 'S')
            {
                sx = i;
                sy = j;
            }
            if(c == 'T')
            {
                tx = i;
                ty = j;
            }
            if(c != '.') ma[P(i, j)] = id++;
        }
    }
    int num = H*W;
    for(int i = 0; i < H; i++)
    {
        for(int j = 0; j < W; j++)
        {
            if(ma.find(P(i, j)) != ma.end())
            {
                int now = ma[P(i, j)];
                if(sx == i && sy == j)
                {
                    add_edge(source, now, INF);
                    add_edge(now, now + num, INF);
                }
                else if(tx == i && ty == j)
                {
                    add_edge(now + num, sink, INF);
                    add_edge(now, now + num, INF);
                }
                else add_edge(now, now + num, 1);
                for(int y = j+1; y < W; y++)
                {
                    if(ma.find(P(i, y)) == ma.end()) continue;
                    int nxid = ma[P(i, y)];
                    add_edge(now + num, nxid, 1);
                    add_edge(nxid + num, now, 1);
                }
                for(int x = i+1; x < H; x++)
                {
                    if(ma.find(P(x, j)) == ma.end()) continue;
                    int nxid = ma[P(x, j)];
                    add_edge(now + num, nxid, 1);
                    add_edge(nxid + num, now, 1);
                }
            }
        }
    }
    if(sx == tx || sy == ty) cout << -1 << endl;
    else cout << max_flow(source, sink) << endl;
}


Measured memory_bytes: 63616