#include<bits/stdc++.h>
#define pb           push_back
#define pii          pair<int,int>
#define vi           vector<int>
#define vpii         vector<pair<int,int>>
#define gph          map<int, vector<int>>
#define mp           make_pair
#define FOR(i, n) for (int i = 0; i <  (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD0(i, n) for (int i = (int)n; i >= 0; i--)
#define FORD1(i, n) for (int i = (int)n; i >= 1; i--)
#define FORS(i, n) for(; i < (int)(n); i++)
#define debug(X) { cout << #X << " = " << (X) << endl; }
#define printArr(A,n) { cout << #A << " = "; FOR(i,n) cout << A[i] << ' '; cout << endl; }
using namespace std;
#define int long long int
int n;
vector<vector<int>> capacity;
vector<vector<int>> adj;
struct FlowEdge {
    int v, u;
    long long cap, flow = 0;
    FlowEdge(int v, int u, long long cap) : v(v), u(u), cap(cap) {}
};

struct Dinic {
    const long long flow_inf = 1e9;
    vector<FlowEdge> edges;
    vector<vector<int>> adj;
    int n, m = 0;
    int s, t;
    vector<int> level, ptr;
    queue<int> q;

    Dinic(int n, int s, int t) : n(n), s(s), t(t) {
        adj.resize(n);
        level.resize(n);
        ptr.resize(n);
    }

    void add_edge(int v, int u, long long cap) {
        edges.emplace_back(v, u, cap);
        edges.emplace_back(u, v, 0);
        adj[v].push_back(m);
        adj[u].push_back(m + 1);
        m += 2;
    }

    bool bfs() {
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (int id : adj[v]) {
                if (edges[id].cap - edges[id].flow < 1)
                    continue;
                if (level[edges[id].u] != -1)
                    continue;
                level[edges[id].u] = level[v] + 1;
                q.push(edges[id].u);
            }
        }
        return level[t] != -1;
    }

    long long dfs(int v, long long pushed) {
        if (pushed == 0)
            return 0;
        if (v == t)
            return pushed;
        for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
            int id = adj[v][cid];
            int u = edges[id].u;
            if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
                continue;
            long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
            if (tr == 0)
                continue;
            edges[id].flow += tr;
            edges[id ^ 1].flow -= tr;
            return tr;
        }
        return 0;
    }

    long long flow() {
        long long f = 0;
        while (true) {
            fill(level.begin(), level.end(), -1);
            level[s] = 0;
            q.push(s);
            if (!bfs())
                break;
            fill(ptr.begin(), ptr.end(), 0);
            while (long long pushed = dfs(s, flow_inf)) {
                f += pushed;
            }
        }
        return f;
    }
};
int bfs(int s, int t, vector<int>& parent) {
    fill(parent.begin(), parent.end(), -1);
    parent[s] = -2;
    queue<pair<int, int>> q;
    q.push({s, 1e9});

    while (!q.empty()) {
        int cur = q.front().first;
        int flow = q.front().second;
        q.pop();
      //  debug(cur);

        for (int next : adj[cur]) {
            if (parent[next] == -1 && capacity[cur][next]) {
                parent[next] = cur;
                int new_flow = min(flow, capacity[cur][next]);
                if (next == t)
                    return new_flow;
                q.push({next, new_flow});
            }
        }
    }

    return 0;
}

int maxflow(int s, int t) {
    int flow = 0;
    vector<int> parent(n);
    int new_flow;

    while (new_flow = bfs(s, t, parent)) {
        flow += new_flow;
        int cur = t;
        while (cur != s) {
            int prev = parent[cur];
            capacity[prev][cur] -= new_flow;
            capacity[cur][prev] += new_flow;
            cur = prev;
        }
    }

    return flow;
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    
    capacity.assign(250, vector<int>(250, 1));
    adj.resize(250);
    int h, w;cin >> h >> w;
    n = 250;
    char in; 
    Dinic d(250, 201, 202);
    const int mx = 1e5;
    FOR(i, h){
        FOR(j, w){
            cin >> in;
     //       debug(in);
            j += 100;
    //        cout << j << " " << i << endl;

            if(in == 'S'){
                d.add_edge(i, 202, mx);
                d.add_edge(j, 202, mx);
                d.add_edge(j, i, 1);
                d.add_edge(i, j, 1);
                d.add_edge(202, i, mx);
                d.add_edge(202, j, mx);
/*                adj[i].pb(202); 
                adj[j].pb(202);
                adj[i].pb(j);
                adj[j].pb(i);
                adj[202].pb(j);
                adj[202].pb(i);
                capacity[202][j]= 10000;
                capacity[202][i]= 10000;
                capacity[i][202]= 10000;
                capacity[j][202]= 10000;*/
            }else if(in == 'T'){
                d.add_edge(i, 201, mx);
                d.add_edge(j, 201, mx);
                d.add_edge(j, i, 1);
                d.add_edge(i, j, 1);
                d.add_edge(201, i, mx);
                d.add_edge(201, j, mx);
/*                adj[i].pb(201); 
                adj[j].pb(201);
                adj[i].pb(j);
                adj[j].pb(i);
                adj[201].pb(j);
                adj[201].pb(i);
                capacity[201][j]= 10000;
                capacity[201][i]= 10000;
                capacity[i][201]= 10000;
                capacity[j][201]= 10000;*/
            }else if(in == 'o'){
                d.add_edge(i, j, 1);
                d.add_edge(j, i, 1);
            }
            j -= 100;
        }
    }
    int ans = d.flow();
    if(ans >= 10000){
        cout << -1;
    }else cout << ans;
}




Measured memory_bytes: 3188