#include "bits/stdc++.h"
using namespace std;
using ll     = long long;
using pii    = pair<int, int>;
using pll    = pair<ll, ll>;
using vi     = vector<int>;
using vl     = vector<ll>;
using vvi    = vector<vi>;
using vvl    = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}
template <class C>
void print(const C &c, std::ostream &os = std::cout) {
    std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " "));
    os << std::endl;
}

template <typename T, bool directed>
struct Dinic {
    struct edge {
        int to;
        T cap;   // capacity of the edge
        int rev; // index for to->from edge
        edge() {}
        edge(int to_, T cap_, int rev_) : to(to_), cap(cap_), rev(rev_) {}
    };
    T inf = numeric_limits<T>::max();
    vector<vector<edge>> G;
    vector<int> level, iter;
    Dinic(int n) : G(n), level(n), iter(n) {}

    void addEdge(int from, int to, int cap) {
        G[from].emplace_back(to, cap, G[to].size());
        G[to].emplace_back(from, directed ? 0 : cap, G[from].size() - 1);
    }
    void bfs(int s) {
        fill(level.begin(), level.end(), -1);
        queue<int> que;
        level[s] = 0;
        que.emplace(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.emplace(e.to);
                }
            }
        }
    }
    T dfs(int v, int t, T 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]) {
                T d = dfs(e.to, t, min(f, e.cap));
                if (d == 0)
                    continue;
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
        return 0;
    }
    T maxFlow(int s, int t, T lim) {
        T fl = 0;
        while (true) {
            bfs(s);
            if (level[t] < 0 || lim == 0)
                break;
            fill(iter.begin(), iter.end(), 0);
            while (true) {
                T f = dfs(s, t, lim);
                if (f == 0)
                    break;
                fl += f;
                lim -= f;
            }
        }
        return fl;
    }
    // returns maximum flow from s to t
    T maxFlow(int s, int t) { return maxFlow(s, t, inf); }
};

int main() {
    int h, w;
    cin >> h >> w;
    vector<string> a(h);
    for (int i = 0; i < h; ++i) {
        cin >> a[i];
    }
    Dinic<int, false> dinic(h + w + 2);
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            if (a[i][j] == 'S') {
                dinic.addEdge(h + w, i, 1 << 30);
                dinic.addEdge(h + w, h + j, 1 << 30);
            }
            if (a[i][j] == 'T') {
                dinic.addEdge(h + w + 1, i, 1 << 30);
                dinic.addEdge(h + w + 1, h + j, 1 << 30);
            }
            if (a[i][j] == 'o') {
                dinic.addEdge(i, h + j, 1);
            }
        }
    }
    ll flow = dinic.maxFlow(h + w, h + w + 1);
    if (flow >= 1 << 30)
        cout << -1 << "\n";
    else
        cout << flow << "\n";
    return 0;
}

Measured memory_bytes: 640