#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;
}


Measured memory_bytes: 28544