#include <bits/stdc++.h>
#include <sys/time.h>

using namespace std;

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <ctime>
#include <set>
#include <cassert>
#include <iostream>
#include <memory>
#include <unordered_map>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <functional>
#include <fstream>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
static const double EPS = 1e-10;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define all(a) a.begin(),a.end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SS stringstream
#define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts("");
#define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");}
#define bitcount(b) __builtin_popcount(b)
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
 
#define delete(a,n) a.erase(remove(all(a),n),a.end())
template<typename T, typename S> vector<T>& operator<<(vector<T>& a, S b) { a.push_back(b); return a; }
template<typename T> void operator>>(vector<T>& a, int b) {while(b--)if(!a.empty())a.pop_back();}
bool isprime(int n){ if(n<2)return false;  for(int i=2;i*i<=n;i++)if(n%i==0)return false;  return true;} 
ll b_pow(ll x,ll n){return n ? b_pow(x*x,n/2)*(n%2?x:1) : 1ll;}
string itos(int n){stringstream ss;ss << n;return ss.str();}
 
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
 
clock_t st,en;
int INF = 3000;
typedef int Weight;
struct Edge {
  short src, dst;
  short weight;
  Edge(short src, short dst, Weight weight) :
    src(src), dst(dst), weight(weight) { }
};
bool operator < (const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
    e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
 
typedef vector<Weight> Array;
typedef unordered_map<short,int> Matrix[20010];


#define RESIDUE(s,t) (capacity[s][t]-flow[s][t])

#define GLOBAL_RELABELING() { \
  queue<int> Q; Q.push(t); \
  fill(ALL(d), INF); d[t] = 0; \
  while (!Q.empty()) { \
    int u = Q.front(); Q.pop(); \
    FOR(e, g[u]) if (RESIDUE(e->dst, u) > 0 && d[u] + 1 < d[e->dst])  \
      Q.push(e->dst), d[e->dst] = d[u] + 1; \
  } \
}
#define PUSH(u, v) { \
  Weight delta = min(excess[u], RESIDUE(u, v)); \
  flow[u][v] += delta; flow[v][w] -= delta; \
  excess[u] -= delta; excess[v] += delta; }
Weight maximumFlow(const Graph &g, int s, int t) {
  int n = g.size(), count = 0;
  Matrix flow,capacity;
  REP(u,n) FOR(e,g[u]) capacity[e->src][e->dst] += e->weight;

   Weight total = 0;
  while (1) {
    queue<int> Q; Q.push(s);
    vector<int> prev(n, -1); prev[s] = s;
    while (!Q.empty() && prev[t] < 0) {
      int u = Q.front(); Q.pop();
      FOR(e,g[u]) if (prev[e->dst] < 0 && RESIDUE(u, e->dst) > 0) {
        prev[e->dst] = u;
        Q.push(e->dst);
      }
    }
    if (prev[t] < 0) return total; // prev[x] == -1 <=> t-side
    Weight inc = INF;
    for (int j = t; prev[j] != j; j = prev[j])
      inc = min(inc, RESIDUE(prev[j], j));
    for (int j = t; prev[j] != j; j = prev[j])
      flow[prev[j]][j] += inc, flow[j][prev[j]] -= inc;
    total += inc;
  }
} 
 
 
int H,W,N;
 
int in(int x){
	return x;
}
int out(int x){
	return x + N;
}
void addEdge(Graph &g,int s,int t,int c){
	//cout << s << " " << t << "|" << c << endl;
	g[s].push_back(Edge(s,t,c));
	g[t].push_back(Edge(t,s,0));
}
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
 


void solve(long long H, long long W, vector<string> S){
	st = clock();
	
	N = H * W;
	int sx,sy,gx,gy;
	bool sp = 1;
	for(int i = 0 ; i < H ; i++){
		for(int j = 0 ; j < W ; j++){
			if( S[i][j] == 'S' ){
				sx = j;
				sy = i;
			}
			if( S[i][j] == 'T' ){
				gx = j;
				gy = i;
			}
			if( S[i][j] == '.' ) sp = 0;
		}
	}


	Graph g(2*N+2);
	int s = g.size() - 1;
	int t = g.size() - 2;
	int cg = 0;
	int cs = 0;
	if( gx == sx || gy == sy ){
		cout << -1 << endl;
		return;
	}
	
	for(int j = 0 ; j < W ; j++)
		if( S[sy][j] == 'o' ){
			int idx1 = sy * W + j;
			addEdge(g,s,in(idx1),INF);
			cs++;
		}
	for(int j = 0 ; j < W ; j++)
		if( S[gy][j] == 'o' ){
			int idx1 = gy * W + j;
			addEdge(g,out(idx1),t,INF);
			cg++;
		}
		
	for(int i = 0 ; i < H ; i++)
		if( S[i][sx] == 'o' ){
			int idx1 = i * W + sx;
			addEdge(g,s,in(idx1),INF);
			cs++;
		}
	for(int i = 0 ; i < H ; i++)
		if( S[i][gx] == 'o' ){
			int idx1 = i * W + gx;
			addEdge(g,out(idx1),t,INF);
			cg++;
		}
	for(int i = 0 ; i < H ; i++){
		for(int j = 0 ; j < W ; j++){
			if( S[i][j] == 'o' ){
				int idx1 = i * W + j;
				addEdge(g,in(idx1),out(idx1),1);
			}
		}
	}	
	if(sp){
		cout << min(cg,cs) << endl;
		return;		
	}
	for(int i = 0 ; i < H ; i++){
		for(int j = 0 ; j < W ; j++){
			for(int J = 0 ; J < W ; J++){
				if( S[i][j] != 'o' ) continue;
				if( S[i][J] != 'o' ) continue;
				int idx1 = i * W + j;				
				int idx2 = i * W + J;
				addEdge(g,out(idx1),in(idx2),INF);				
			}
			for(int I = 0 ; I < H ; I++){
				if( S[i][j] != 'o' ) continue;
				if( S[I][j] != 'o' ) continue;
				int idx1 = i * W + j;				
				int idx2 = I * W + j;
				addEdge(g,out(idx1),in(idx2),INF);				
			}	
		}
	}

	int res = maximumFlow(g,s,t);

	
	//	cout << res << endl;
	if( res >= INF ){
		cout << -1 << endl;
	}else{
		cout << res << endl;
	}

}

int main(){	
	long long W;
	long long H;
	scanf("%lld",&H);
	vector<string> a(H-1+1);
	scanf("%lld",&W);
	for(int i = 0 ; i <= H-1 ; i++){
		cin >> a[i];
	}
	solve(H, W, a);
	return 0;
}


Measured memory_bytes: 59008