#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
using namespace std;

template <typename T> class Dinic {
    struct edge {
        int to, rev;
        T cap;
        bool isrev;
        edge(int t, T c, int r, bool i) : to(t), cap(c), rev(r), isrev(i) {}
    };

    void bfs(int s) {
        queue<int> que;
        lev.assign(g.size(), -1);
        lev[s] = 0;
        que.emplace(s);
        while (!que.empty()) {
            int v = que.front();
            que.pop();
            for (edge& e : g[v]) {
                if (lev[e.to] < 0 && e.cap > 0) {
                    lev[e.to] = lev[v] + 1;
                    que.emplace(e.to);
                }
            }
        }
    }

    T dfs(int v, int t, T f) {
        if (v == t) return f;
        for (int& i = ite[v]; i < g[v].size(); ++i) {
            edge& e = g[v][i];
            if (e.cap > 0 && lev[v] < lev[e.to]) {
                T 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;
    }

public:

    const T INF;
    vector<int> lev, ite;
    vector<vector<edge>> g;

    Dinic(int n) : INF(numeric_limits<T>::max()), g(n) {}

    void add_edge(int f, int t, T c = numeric_limits<T>::max()) {
        g[f].emplace_back(t, c, g[t].size(), false);
        g[t].emplace_back(f, 0, g[f].size() - 1, true);
    }

    T max_flow(int s, int t) {
        lev.assign(g.size(), -1);
        T res = 0;
        while (1) {
            bfs(s);
            if (lev[t] < 0) break;
            ite.assign(g.size(), 0);
            T f = 0;
            while ((f = dfs(s, t, INF)) > 0) {
                res += f;
            }
        }
        return res;
    }

    void print() {
        for (int i = 0; i < g.size(); ++i) {
            for (auto& e : g[i]) {
                if (e.isrev) continue;
                auto& rev_e = g[e.to][e.rev];
                cout << i << " -> " << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
            }
        }
    }
};

int H, W;
string s[100];
int sy, sx, ty, tx;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> H >> W;
    REP (i, H) {
        cin >> s[i];
        REP (j, W) {
            if (s[i][j] == 'S') sy = i, sx = j, s[i][j] = 'o';
            if (s[i][j] == 'T') ty = i, tx = j, s[i][j] = 'o';
        }
    }
    if (sy == ty || sx == tx) {
        cout << -1 << endl;
        return 0;
    }
    Dinic<int> g(2 * H * W);
    REP (i, H) REP (j, W) {
        g.add_edge(2 * (i * W + j), 2 * (i * W + j) + 1, 1);
    }
    REP (i, H) REP (j, W) if (s[i][j] == 'o') {
        for (int k = 0; k < H; ++k) {
            if (k != i && s[k][j] == 'o') {
                g.add_edge(2 * (i * W + j) + 1, 2 * (k * W + j), 1);
            }
        }
        for (int k = 0; k < W; ++k) {
            if (k != j && s[i][k] == 'o') {
                g.add_edge(2 * (i * W + j) + 1, 2 * (i * W + k), 1);
            }
        }
    }
    cout << g.max_flow(2 * (sy * W + sx) + 1, 2 * (ty * W + tx)) << endl;
}

Measured memory_bytes: 165888