#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...) 0
#endif

template <typename T>
struct dinic {
    T eps = (T)1e-9;

    struct edge {
        int from;
        int to;
        T c;
        T f;
    };

    vector<vector<int>> g;
    vector<edge> edges;
    vector<int> ptr;
    vector<int> d;
    vector<int> q;
    int n;
    int st;
    int fin;
    T flow;

    dinic(int _n, int _st, int _fin)
        : n(_n), st(_st), fin(_fin) {
        g.resize(n);
        ptr.resize(n);
        d.resize(n);
        q.resize(n);
        flow = 0;
    }

    void clear_flow() {
        for (const edge &e : edges) {
            e.f = 0;
        }
        flow = 0;
    }

    int add(int from, int to, T forward_cap, T backward_cap = 0) {
        int id = (int)edges.size();
        g[from].push_back(id);
        edges.push_back({from, to, forward_cap, 0});
        g[to].push_back(id + 1);
        edges.push_back({to, from, backward_cap, 0});
        return id;
    }

    bool expath() {
        fill(d.begin(), d.end(), -1);
        q[0] = fin;
        d[fin] = 0;
        int beg = 0, end = 1;
        while (beg < end) {
            int i = q[beg++];
            for (int id : g[i]) {
                const auto &e = edges[id];
                const auto &back = edges[id ^ 1];
                if (back.c - back.f > eps && d[e.to] == -1) {
                    d[e.to] = d[i] + 1;
                    if (e.to == st) {
                        return true;
                    }
                    q[end++] = e.to;
                }
            }
        }
        return false;
    }

    T dfs(int v, T w) {
        if (v == fin) {
            return w;
        }
        int &j = ptr[v];
        while (j >= 0) {
            int id = g[v][j];
            const auto &e = edges[id];
            if (e.c - e.f > eps && d[e.to] == d[v] - 1) {
                T t = dfs(e.to, min(e.c - e.f, w));
                if (t > eps) {
                    edges[id].f += t;
                    edges[id ^ 1].f -= t;
                    return t;
                }
            }
            j--;
        }
        return 0;
    }

    T max_flow() {
        while (expath()) {
            for (int i = 0; i < n; i++) {
                ptr[i] = (int)g[i].size() - 1;
            }
            T big_add = 0;
            while (true) {
                T add = dfs(st, numeric_limits<T>::max());
                if (add <= eps) {
                    break;
                }
                big_add += add;
            }
            if (big_add <= eps) {
                break;
            }
            flow += big_add;
        }
        return flow;
    }

    vector<bool> min_cut() {
        max_flow();
        vector<bool> res(n);
        for (int i = 0; i < n; i++) {
            res[i] = (d[i] != -1);
        }
        return res;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    for (int i = 0; i < h; i++) {
        cin >> s[i];
    }
    vector<vector<int>> a(h, vector<int>(w, -1));
    int id = 0, st, fn;
    int xx = 0, yy = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (s[i][j] == 'o') {
                a[i][j] = id;
                id++;
            } else if (s[i][j] == 'S') {
                xx ^= i, yy ^= j;
                a[i][j] = id;
                st = id;
                id++;
            } else if (s[i][j] == 'T') {
                xx ^= i, yy ^= j;
                a[i][j] = id;
                fn = id;
                id++;
            }
        }
    }
    if (xx * yy == 0) {
        cout << -1 << endl;
        return 0;
    }
    dinic<int> nt(id * 2, st + id, fn);
    for (int i = 0; i < id; i++) {
        nt.add(i, i + id, 1);
    }
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (a[i][j] == -1) continue;
            for (int k = i + 1; k < h; k++) {
                if (a[k][j] != -1) {
                    nt.add(a[i][j] + id, a[k][j], 1);
                    nt.add(a[k][j] + id, a[i][j], 1);
                }
            }
            for (int k = j + 1; k < w; k++) {
                if (a[i][k] != -1) {
                    nt.add(a[i][j] + id, a[i][k], 1);
                    nt.add(a[i][k] + id, a[i][j], 1);
                }
            }
        }
    }
    cout << nt.max_flow() << endl;
    return 0;
}

Measured memory_bytes: 85468