#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define _repargs(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _repargs(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template
struct point {int x, y;};

// goal, capacity, index of reverse edge
struct edge {
    int to, cap, rev;
};

// O(EV^2)
class Dinic{
public:
    int V;
    vector<vector<edge>> G;
    vector<int> L;
    vector<int> I;
    
    Dinic(int v){
        G.resize(v), L.resize(v), I.resize(v);
        V = v;
    }
    
    // add edge
    void add(int from, int to, int cap){
        G[from].push_back((edge){to, cap, (int)G[to].size()});
        G[to].push_back((edge){from, 0, (int)G[from].size() - 1});
    }
    
    // label dist from s
    void bfs(int s){
        L.assign(V, -1);
        queue<int> q;
        L[s] = 0;
        q.push(s);
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            rep(i, G[v].size()){
                edge &e = G[v][i];
                if (e.cap > 0 && L[e.to] < 0) {
                    L[e.to] = L[v] + 1;
                    q.push(e.to);
                }
            }
        }
    }
    
    // search positive path
    int dfs(int v, int t, int f){
        if (v == t) return f;
        for (int &i = I[v]; i < G[v].size(); i++) {
            edge &e = G[v][i];
            if (e.cap > 0 && L[v] < L[e.to]) {
                int d = dfs(e.to, t, min(f, e.cap));
                if (d) {
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }
    
    // calc max flow
    int max_flow(int s, int t){
        int flow = 0;
        for (;;) {
            bfs(s);
            if (L[t] < 0) return flow;
            I.assign(V, 0);
            int f;
            while ((f = dfs(s, t, inf)) > 0) {
                flow += f;
            }
        }
    }
};

int H, W;

int f(int h, int w){
    return W * h + w;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    
    cin >> H >> W;
    vector<string> A(H);
    rep(i, H) cin >> A[i];
    
    point S, G;
    
    rep(i, H) rep(j, W){
        if(A[i][j] == 'S') S = point({j, i});
        if(A[i][j] == 'T') G = point({j, i});
    }
    
//    cout << S.x << "," << S.y << "," <<  G.x << "," << G.y << endl;
    if(S.x == G.x || S.y == G.y){
        output(-1);
        return 0;
    }
    
    Dinic D(2 + H + W);
    
    // S: 0, 縦: 1-H, 横:H+1-H+W, G: H+W+1
    
    rep(i, H){
        rep(j, W) {
            if(A[i][j] == 'S'){
                D.add(0, i + 1, inf);
                D.add(0, H + j + 1, inf);
            }
            if(A[i][j] == 'T'){
                D.add(i + 1, H + W + 1, inf);
                D.add(H + j + 1, H + W + 1, inf);
            }
            if(A[i][j] == 'o'){
                D.add(i + 1, H + j + 1, 1);
                D.add(H + j + 1, i + 1, 1);
            }
        }
    }
    int ans = D.max_flow(0, H + W + 1);
    output(ans);
    
    return 0;
}

Measured memory_bytes: 896