#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(ll i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

template<typename T> class Dinic {
private:
    int V;
    vector<int> level,iter;
    void bfs(int s) {
        fill(level.begin(),level.end(),-1);
        queue<int> que;
        level[s] = 0;
        que.push(s);
        while(!que.empty()){
            int v = que.front();
            que.pop();
            for(auto& e : G[v]){
                if(e.cap > 0 && level[e.to] < 0){
                    level[e.to] = level[v] + 1;
                    que.push(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){
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

public:
    struct edge{
        int to;
        T cap;
        int rev;
    };
    vector<vector<edge> > G;

    Dinic(int node_size) : V(node_size), level(V), iter(V), G(V){}
    //辺を張る
    void add_edge(int from,int to,T cap) {
        G[from].push_back((edge){to,cap,(int)G[to].size()});
        G[to].push_back((edge){from,(T)0,(int)G[from].size()-1});
    }
    //最大流を計算
    T solve(int s,int t) {
        T flow = 0;
        for(;;){
            bfs(s);
            if(level[t] < 0) return flow;
            fill(iter.begin(),iter.end(),0);
            T f;
            while((f=dfs(s,t,numeric_limits<T>::max())) > 0){
                flow += f;
            }
        }
    }
};

ll h, w, si, sj, ti, tj;

ll encode(ll x, ll y, ll f){
	return x*w+y+f*h*w;
}

int main(){
	cin.tie(0); ios::sync_with_stdio(false);
	
	cin >> h >> w;
	string a[h];
	zep(i, 0, h){
		cin >> a[i];
		zep(j, 0, w){
			if(a[i][j] == 'S'){si = i; sj = j; a[i][j] = 'o';}
			if(a[i][j] == 'T'){ti = i; tj = j; a[i][j] = 'o';}
		}
	}
	//print(encode(si, sj, 1))
	//print(encode(ti, tj, 1))
	Dinic<ll> d(2*h*w);
	zep(i, 0, h){
		zep(j, 0, w){
			d.add_edge(encode(i, j, 0), encode(i, j, 1), 1);
			//d.add_edge(encode(i, j, 1), encode(i, j, 1), 0);
		}
	}
	
	zep(i, 0, h){
		zep(j, 0, w){
			zep(k, j+1, w){
				if(a[i][j] == 'o' && a[i][k] == 'o'){
					d.add_edge(encode(i, j, 1), encode(i, k, 0), 1);
					d.add_edge(encode(i, k, 1), encode(i, j, 0), 1);
					//cout << i << j << k << endl;
				}
			}
		}
	}
	
	zep(j, 0, w){
		zep(i, 0, h){
			zep(k, i+1, h){
				if(a[i][j] == 'o' && a[k][j] == 'o'){
					d.add_edge(encode(i, j, 1), encode(k, j, 0), 1);
					d.add_edge(encode(k, j, 1), encode(i, j, 0), 1);
					//cout << i << j << k << endl;
				}
			}
		}
	}

	if(si == ti || sj == tj){
		print(-1)
	}
	else{
		print(d.solve(encode(si, sj, 1), encode(ti, tj, 0)))
	}
	return 0;
}

Measured memory_bytes: 122112