#include <iostream>
#include <vector>
#include <cstring>

using namespace std;
const int MAX_V=10010;
const long long INF=10000000;
struct edge{int to,cap,rev;};
vector<edge> G[MAX_V];
int used[MAX_V];

void add_edge(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});
	G[to].push_back((edge){from,cap,(int)G[from].size()});
	G[from].push_back((edge){to,0,(int)G[to].size()-1});
}

int dfs(int v, int t, int f){
	if(v==t) return f;
	used[v] = true;
	for(int i=0;i<G[v].size();i++){
		edge &e = G[v][i];
		if(!used[e.to] && e.cap>0){
			int 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;
}

int max_flow(int s, int t){
	int flow = 0;
	for(;;){
		memset(used,0,sizeof(used));
		int f = dfs(s,t,INF);
		if(f==0) return flow;
		flow += f;
	}
}

int main(){
	int i,j,h,w,s,t;
	char a[10010];
	cin >> h >> w;
	for(i=0;i<h*w;i++){
		cin >> a[i];
		if(a[i]=='S') s = i;
		if(a[i]=='T') t = i;
	}
	if(s%w==t%w || s/w == t/w){
		cout << -1 << endl;
		return 0;
	}
	for(i=0;i<h*w;i++){
		if(a[i]=='S'){
			add_edge(1000,i/w,INF); add_edge(1000,i%w+200,INF);
		}
		if(a[i]=='T'){
			add_edge(i/w,2000,INF); add_edge(i%w+200,2000,INF);
		}
		if(a[i]=='o'){
			add_edge(i/w,i%w+200,1);
		}
	}
	
	cout << max_flow(1000,2000) << endl;
}
