#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

class Dinic {
    struct edge {
        i64 to, cap, rev;
    };

    int n;
    vector<vector<edge>> G;
    vi level;
    vi iter;

    void bfs(int s) {
        level.assign(n, -1);
        queue<int> que;
        que.push(s);
        level[s] = 0;
        while (que.size()) {
            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);
                }
            }
        }
    }

    i64 dfs(int v, int t, i64 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]) {
                i64 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:
    Dinic(int n) : n(n), G(n), level(n), iter(n) {}

    void addEdge(int from, int to, i64 cap) {
        G[from].push_back({to, cap, int(G[to].size())});
        G[to].push_back({from, 0, int(G[from].size()) - 1});
    }

    i64 max_flow(int s, int t) {
        i64 flow = 0;
        while (1) {
            bfs(s);
            if (level[t] < 0) return flow;
            iter.assign(n, 0);
            i64 f;
            while ((f = dfs(s, t, 1e18)) > 0) {
                flow += f;
            }
        }
    }
};

int h, w;

int id(int x, int y) {
    return x * w + y;
}

int sx, sy, gx, gy;

bool is_s(int x, int y) {
    return x == sx && y == sy;
}

bool is_g(int x, int y) {
    return x == gx && y == gy;
}

int main() {
    cin >> h >> w;
    vvi b(h, vi(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            char c;
            cin >> c;
            if (c == 'S') {
                sx = i, sy = j;
                b[i][j] = 1;
            } else if (c == 'T') {
                gx = i, gy = j;
                b[i][j] = 1;
            } else if (c == 'o') {
                b[i][j] = 1;
            }
        }
    }

    if (sx == gx || sy == gy) {
        cout << -1 << endl;
        return 0;
    }

    int n = h * w;
    Dinic d(n * 2);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (!b[i][j] || is_g(i, j)) continue;
            for (int k = 0; k < w; k++) {
                if (k != j && b[i][k] && !is_s(i, k)) {
                    d.addEdge(id(i, j) + n, id(i, k), 1);
                }
            }
            for (int k = 0; k < h; k++) {
                if (i != k && b[k][j] && !is_s(k, j)) {
                    d.addEdge(id(i, j) + n, id(k, j), 1);
                }
            }
        }
    }

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (b[i][j]) {
                if (!is_s(i, j) && !is_g(i, j)) {
                    d.addEdge(id(i, j), id(i, j) + n, 1);
                    d.addEdge(id(i, j) + n, id(i, j), 1);
                }
            }
        }
    }

    cout << d.max_flow(id(sx, sy) + n, id(gx, gy)) << endl;
}

Measured memory_bytes: 124672