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++ (GCC 9.2.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#define REP(i,n)   for(int i=0;i<(n);++i)
#define ALL(v)     (v).begin(),(v).end()
#define debug(x)   cerr<<#x<<": "<<(x)<<endl
using namespace std;
using llong = long long;
using vi    = vector<int>;
using vvi   = vector<vi >;
using vvvi  = vector<vvi >;
using pii   = pair<int,int>;
constexpr int    INF=1e9;
constexpr double EPS=1e-9;
constexpr int    MOD=1e9+7;
template<class Type>
void line(const Type &a){int cnt=0;for(const auto &elem:a){cerr<<(cnt++?' ':'>');cerr<<elem;}cerr<<endl;}

template<typename Type>
struct Dinic{
	struct Edge{				//Edge:=(辺の情報を表す構造体).
		int to;//to:=(行先). 
		Type cap;//cap:=(容量).
		int rev;//rev:=(逆辺).
	};
	
	vector<vector<Edge> > g;	//g[i]:=(ノードiの隣接リスト).
	vi level;					//level[i]:=(sからiまでの距離).
	vi iter;					//iter[i]:=(ノードiの隣接ノードをどこまで調べたか).
	Type inf;
	
	Dinic(int vn):g(vn),level(vn),iter(vn){//constructor.
		if(typeid(Type)==typeid(int)) inf=1e9;
		else if(typeid(Type)==typeid(long long)) inf=1e18;
	}
	
	void add_egde(int from,int to,Type cap){//fromからtoへ向かう容量capの辺をグラフに追加する.
		g[from].push_back((Edge){to,cap,(int)g[to].size()});
		g[to].push_back((Edge){from,0,(int)g[from].size()-1});
	}
	void bfs(int s){//sからの最短距離を計算する.
		fill(ALL(level),-1);
		level[s]=0;
		queue<int> que;
		que.push(s);
		while(!que.empty()){
			int v=que.front();
			que.pop();
			REP(i,g[v].size()){
				Edge &e=g[v][i];
				if(e.cap>0 and level[e.to]<0){
					level[e.to]=level[v]+1;
					que.push(e.to);
				}
			}
		}	
	}
	Type dfs(int v,int t,Type f){//増加パスを探す.
		if(v==t) return f;
		for(int &i=iter[v];i<g[v].size();++i){
			Edge &e=g[v][i];
			if(e.cap>0 and level[v]<level[e.to]){
				Type 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;
	}
	Type max_flow(int s,int t){//sからtへの最大流を求める. O(|E|*(|V|^2)).
		Type flow=0;
		while(1){
			bfs(s);
			if(level[t]<0) return flow;
			fill(ALL(iter),0);
			Type f;
			while((f=dfs(s,t,inf))>0) flow+=f;
		}
	}
};

int main(){//Refering other editorials.
	int h,w;
	cin>>h>>w;
	
	vector<string> a(h);
	REP(i,h) cin>>a[i];
	
	int sy,sx,ty,tx;
	REP(y,h)REP(x,w){
		if(a[y][x]=='S') sy=y, sx=x;
		else if(a[y][x]=='T') ty=y, tx=x;
	}
	if(sy==ty or sx==tx){//sからtへ一跳び!
		cout<<-1<<endl;
		return 0;
	}
	
	Dinic<int> ob(h+w+2);//(s,t):=(h+w,h+w+1).
	ob.add_egde(h+w,sy,INF);
	ob.add_egde(h+w,h+sx,INF);
	ob.add_egde(ty,h+w+1,INF);
	ob.add_egde(h+tx,h+w+1,INF);
	REP(y,h)REP(x,w){
		if(a[y][x]=='o'){
			ob.add_egde(y,h+x,1);
			ob.add_egde(h+x,y,1);
		}
	}
	
	cout<<ob.max_flow(h+w,h+w+1)<<endl;
	return 0;
}