#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

struct Dinic{
	struct edge{
		ll to, cap, rev;
	};
	vector<vector<edge>> G;
	vector<int> level, itr;
	ll n;
	const ll INF = 1<<30;
 
	Dinic(ll n) : n(n){
		G.resize(n);
		level.resize(n);
		itr.resize(n);
	}
 
	void add_edge(ll from, ll to, ll cap){
		G[from].push_back({to,cap,(ll)G[to].size()});
		G[to].push_back({from,0,(ll)G[from].size()-1});
	}
 
	void bfs(int s){
		fill(level.begin(), level.end(), -1);
		queue<int> q;
		level[s] = 0;
		q.push(s);
		while(!q.empty()){
			int u = q.front(); q.pop();
			for(auto &e : G[u]){
				if(e.cap > 0 && level[e.to] < 0){
					level[e.to] = level[u] + 1;
					q.push(e.to);
				}
			}
		}
	}

	ll dfs(int u, int t, ll f){
		if(u == t) return f;
		for(int &i = itr[u]; i < G[u].size(); i++){
			edge &e = G[u][i];
			if(e.cap > 0 && level[u] < level[e.to]){
				ll 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;
	}
 
	ll max_flow(ll s, ll t){
		ll flow = 0;
		while(1){
			bfs(s);
			if(level[t] < 0) return flow;
			fill(itr.begin(), itr.end(), 0);
			ll f;
			while((f = dfs(s, t, INF)) > 0){
				flow += f;
			}
		}
	}
};

char a[101][101];

int main(){
	ll h,w; cin >> h >> w;
	rep(i,h) rep(j,w) cin >> a[i][j];
	Dinic flow(h+w+2);
	P s,t;
	rep(i,h){
		rep(j,w){
			if(a[i][j] == '.') continue;
			if(a[i][j] == 'S') s = P(i,j);
			if(a[i][j] == 'T') t = P(i,j);
			else{
				flow.add_edge(i, h+j, 1);
				flow.add_edge(h+j, i, 1);
			}
		}
	}
	flow.add_edge(h+w, s.first, inf);
	flow.add_edge(h+w, h+s.second, inf);
	flow.add_edge(t.first, h+w+1, inf);
	flow.add_edge(h+t.second, h+w+1, inf);
	int ans = flow.max_flow(h+w, h+w+1);
	if(ans >= h+w) puts("-1");
	else cout << ans << "\n";
}


Measured memory_bytes: 4544