#include "math.h"
#include <algorithm>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define ifor(i, a, b) for (int i = (a); i < (b); i++)
#define rfor(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef long double ld;
typedef long long int lli;
typedef complex<double> P;
const double eps = 1e-11;
int vex[4] = {1, 0, -1, 0};
int vey[4] = {0, 1, 0, -1};
typedef vector<double> Vec;
typedef vector<int> vec;
typedef vector<Vec> MAT;
typedef vector<vec> mat;
lli MOD = 1000000007;
int INF = 100000000;
struct edge {
    int to, cap, rev;
    edge(int to, int cap, int rev)
    {
        this->to = to;
        this->cap = cap;
        this->rev = rev;
    }
};

#define MAX_V 100005
vector<edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];
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));
}
lli gcd(lli a, lli b)
{
    return b > a ? gcd(b, a) : (b == 0 ? a : gcd(b, a % b));
}
void bfs(int s)
{
    //memset(level, -1, sizeof(level));
    rep(i, MAX_V) level[i] = -1;
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        for (int i = 0; i < G[v].size(); i++) {
            edge& e = G[v][i];
            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;
        //memset(iter, 0, sizeof(iter));
        rep(i, MAX_V) iter[i] = 0;
        int f;
        while ((f = dfs(s, t, INF)) > 0) {
            flow += f;
        }
    }
}
void init()
{
    rep(i, 2005)
    {
        G[i].clear();
        level[i] = 0;
        iter[i] = 0;
    }
}
int main()
{
    int h, w;
    char a;
    cin >> h >> w;
    int sox, tox, soy, toy;
    int start = h + w;
    int end = h + w + 1;
    rep(i, h) rep(j, w)
    {
        cin >> a;
        if (a == 'S') {
            add_edge(start, i, 1e8);
            add_edge(start, j + h, 1e8);
            sox = i, soy = j;
        } else if (a == 'T') {
            add_edge(i, end, 1e8);
            add_edge(j + h, end, 1e8);
            tox = i, toy = j;
        }

        if (a != '.') {
            add_edge(i, j + h, 1);
            add_edge(j + h, i, 1);
        }
    }
    int ans = max_flow(start, end);
    cout << ((sox == tox or soy == toy)
                    ? -1
                    : ans)
         << endl;
}


Measured memory_bytes: 3968