Problem:
Score : 800 points

Problem Statement

There is a pond with a rectangular shape.
The pond is divided into a grid with H rows and W columns of squares.
We will denote the square at the i-th row from the top and j-th column from the left by (i,\ j).

Some of the squares in the pond contains a lotus leaf floating on the water.
On one of those leaves, S, there is a frog trying to get to another leaf T.
The state of square (i,\ j) is given to you by a character a_{ij}, as follows:

. : A square without a leaf.

o : A square with a leaf floating on the water.

S : A square with the leaf S.

T : A square with the leaf T.

The frog will repeatedly perform the following action to get to the leaf T: "jump to a leaf that is in the same row or the same column as the leaf where the frog is currently located."

Snuke is trying to remove some of the leaves, other than S and T, so that the frog cannot get to the leaf T.
Determine whether this objective is achievable. If it is achievable, find the minimum necessary number of leaves to remove.

Constraints

2 ≤ H, W ≤ 100

a_{ij} is ., o, S or T.

There is exactly one S among a_{ij}.

There is exactly one T among a_{ij}.

Input

Input is given from Standard Input in the following format:

H W
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}

Output

If the objective is achievable, print the minimum necessary number of leaves to remove.
Otherwise, print -1 instead.

Sample Input 1

3 3
S.o
.o.
o.T

Sample Output 1

2

Remove the upper-right and lower-left leaves.

Sample Input 2

3 4
S...
.oo.
...T

Sample Output 2

0

Sample Input 3

4 3
.S.
.o.
.o.
.T.

Sample Output 3

-1

Sample Input 4

10 10
.o...o..o.
....o.....
....oo.oo.
..oooo..o.
....oo....
..o..o....
o..o....So
o....T....
....o.....
........oo

Sample Output 4

5

Solution in C++14 (GCC 5.4.1) (judge verdict: Memory Limit Exceeded)

#include <bits/stdc++.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()
 
int INF = 100000;
typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int 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<int,unordered_map<int,int>> Matrix;
 
#define RESIDUE(s,t) (capacity[s][t]-flow[s][t])
Weight augment(const Graph &g, Matrix &capacity, Matrix &flow,
    const vector<int> &level, vector<bool> &finished, int u, int t, Weight cur) {
  if (u == t || cur == 0) return cur;
  if (finished[u]) return 0;
  finished[u] = true;
  FOR(e, g[u]) if (level[e->dst] > level[u]) {
    Weight f = augment(g, capacity, flow, level, finished,
        e->dst, t, min(cur, RESIDUE(u, e->dst)));
    if (f > 0) {
      flow[u][e->dst] += f; flow[e->dst][u] -= f;
      finished[u] = false;
      return f;
    }
  }
  return 0;
}
Weight maximumFlow(const Graph &g, int s, int t) {
  int n = g.size();
  Matrix flow,capacity;
  REP(u,n) FOR(e,g[u]) capacity[e->src][e->dst] += e->weight;
 
  Weight total = 0;
  for (bool cont = true; cont; ) {
    cont = false;
    vector<int> level(n, -1); level[s] = 0; // make layered network
    queue<int> Q; Q.push(s);
    for (int d = n; !Q.empty() && level[Q.front()] < d; ) {
      int u = Q.front(); Q.pop();
      if (u == t) d = level[u];
      FOR(e, g[u]) if (RESIDUE(u,e->dst) > 0 && level[e->dst] == -1)
        Q.push(e->dst), level[e->dst] = level[u] + 1;
    }
    vector<bool> finished(n); // make blocking flows
    for (Weight f = 1; f > 0; ) {
      f = augment(g, capacity, flow, level, finished, s, t, INF);
      if (f == 0) break;
      total += f; cont = true;
    }
  }
  return total;
}
 
 
 
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){
	
	N = H * W;
	int sx,sy,gx,gy;
	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;
			}
		}
	}


	Graph g(2*N+2);
	int s = g.size() - 1;
	int t = g.size() - 2;
	
	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);
		}
	for(int j = 0 ; j < W ; j++)
		if( S[gy][j] == 'o' ){
			int idx1 = gy * W + j;
			addEdge(g,out(idx1),t,INF);
		}
		
	for(int i = 0 ; i < H ; i++)
		if( S[i][sx] == 'o' ){
			int idx1 = i * W + sx;
			addEdge(g,s,in(idx1),INF);
		}
	for(int i = 0 ; i < H ; i++)
		if( S[i][gx] == 'o' ){
			int idx1 = i * W + gx;
			addEdge(g,out(idx1),t,INF);
		}
	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);
			}
		}
	}	
		
	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 >= 100000 ){
		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;
}