#include <bits/stdc++.h>
 
using namespace std;
 
#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define fileio freopen("in.in", "r", stdin),freopen("out.out", "w", stdout);
#define ll long long int
#define FF first
#define SS second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define sd(x) scanf("%d",&x)
#define slld(x) scanf("%lld",&x)
#define pd(x) printf("%d\n",x)
#define plld(x) printf("%lld\n",x)
#define pss printf
#define MOD 1000000007
#define INF 1e8
#define eps 0.00001
#define endl '\n'
#define debug(n1) cout<<n1<<endl
ll n,m,a[105][105],ei,ej,si,sj;
string s;
ll get(ll i,ll j)
{
	return ((i-1)*m+j);
}


typedef long long LL;

struct Edge {
  int from, to, cap, flow, index;
  Edge(int from, int to, int cap, int flow, int index) :
    from(from), to(to), cap(cap), flow(flow), index(index) {}
};

struct PushRelabel {
  int N;
  vector<vector<Edge> > G;
  vector<LL> excess;
  vector<int> dist, active, count;
  queue<int> Q;

  PushRelabel(int N) : N(N), G(N), excess(N), dist(N), active(N), count(2*N) {}

  void AddEdge(int from, int to, int cap) {
    G[from].push_back(Edge(from, to, cap, 0, G[to].size()));
    if (from == to) G[from].back().index++;
    G[to].push_back(Edge(to, from, 0, 0, G[from].size() - 1));
  }

  void Enqueue(int v) { 
    if (!active[v] && excess[v] > 0) { active[v] = true; Q.push(v); } 
  }

  void Push(Edge &e) {
    int amt = int(min(excess[e.from], LL(e.cap - e.flow)));
    if (dist[e.from] <= dist[e.to] || amt == 0) return;
    e.flow += amt;
    G[e.to][e.index].flow -= amt;
    excess[e.to] += amt;    
    excess[e.from] -= amt;
    Enqueue(e.to);
  }
  
  void Gap(int k) {
    for (int v = 0; v < N; v++) {
      if (dist[v] < k) continue;
      count[dist[v]]--;
      dist[v] = max(dist[v], N+1);
      count[dist[v]]++;
      Enqueue(v);
    }
  }

  void Relabel(int v) {
    count[dist[v]]--;
    dist[v] = 2*N;
    for (int i = 0; i < G[v].size(); i++) 
      if (G[v][i].cap - G[v][i].flow > 0)
	dist[v] = min(dist[v], dist[G[v][i].to] + 1);
    count[dist[v]]++;
    Enqueue(v);
  }

  void Discharge(int v) {
    for (int i = 0; excess[v] > 0 && i < G[v].size(); i++) Push(G[v][i]);
    if (excess[v] > 0) {
      if (count[dist[v]] == 1) 
	Gap(dist[v]); 
      else
	Relabel(v);
    }
  }

  LL GetMaxFlow(int s, int t) {
    count[0] = N-1;
    count[N] = 1;
    dist[s] = N;
    active[s] = active[t] = true;
    for (int i = 0; i < G[s].size(); i++) {
      excess[s] += G[s][i].cap;
      Push(G[s][i]);
    }
    
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      active[v] = false;
      Discharge(v);
    }
    
    LL totflow = 0;
    for (int i = 0; i < G[s].size(); i++) totflow += G[s][i].flow;
    return totflow;
  }
};



int main() {
cin>>n>>m;
for(int i=1;i<=n;i++)
{
	cin>>s;
	for(int j=1;j<=m;j++)
	{
		if(s[j-1]=='o')a[i][j]=1;
		else if(s[j-1]=='S')si=i,sj=j,a[i][j]=1;
		else if(s[j-1]=='T')ei=i,ej=j,a[i][j]=1;
	}
}
PushRelabel pr(2*n*m+1);
pr.AddEdge(get(si,sj),n*m+get(si,sj),INF);
pr.AddEdge(get(ei,ej),n*m+get(ei,ej),INF);
for(int i=1;i<=n;i++)
{
	for(int j=1;j<=m;j++)
	{
		if(a[i][j]&&get(i,j)!=get(si,sj)&&get(i,j)!=get(ei,ej))
			pr.AddEdge(get(i,j),n*m+get(i,j),1);
		if(a[i][j])
		{
			for(int k=1;k<=m;k++)
				if(a[i][k])
					pr.AddEdge(n*m+get(i,j),get(i,k),INF);
			for(int k=1;k<=n;k++)
				if(a[k][j])
					pr.AddEdge(n*m+get(i,j),get(k,j),INF);	
		}
	}
}
ll x=pr.GetMaxFlow(get(si,sj),n*m+get(ei,ej));
if(x>=INF)
	x=-1;
cout<<x<<endl;
	return 0;
           } 

Measured memory_bytes: 104448