#include <bits/stdc++.h>
#define ll long long
#define REP(i, n) for (ll i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (ll i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb push_back
#define debug(x) cerr << #x << ": " << (x) << endl
#define debug2(x, y) cerr << #x << ": " << (x) << " " << #y << ": " << y << endl;
#define int long long
using namespace std;
using II = pair<int, int>;
using VII = vector<II>;
using VI = vector<int>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
template <class T = int> inline T in() { T x; cin >> x; return x; }
template <class T = int> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <class T = int> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <class T> ostream& operator<<(ostream &s, const vector<T>& d) { int n = d.size(); REP (i, n) s << d[i] << " "; return s; }
template <class T> ostream& operator<<(ostream &s, const vector<vector<T>>& dd) { for (vector<T> d: dd) s << d << endl; return s; }
struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast;
const int MOD = 1e9 + 7;

// Dinic法 O(E * V^2)
class Graph {
	int t; // ゴール
	VI level, iter;
	void bfs(int s) {
		for (int i = 0; i < level.size(); i++) level[i] = -1;
		queue<int> que;
		level[s] = 0;
		que.push(s);
		while (!que.empty()) {
			int v = que.front(); que.pop();
			for (int i = 0; i < G[v].size(); i++) {
				edge &e = G[v][i];
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					que.push(e.to);
				}
			}
		}
	}
	int dfs(int v, int 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 && level[v] < level[e.to]) {
				int d = dfs(e.to, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					G[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}
public:
  int V;
  struct edge {int to, cap, rev; };
	vector<vector<edge>> G;
  Graph(int V) : V(V), G(V), level(V), iter(V) {}

	void add_edge(int from, int to, int cap) {
		G[from].push_back(edge{ to, cap, (int)G[to].size() });
		G[to].push_back(edge{ from, 0, (int)G[from].size() - 1 });
	}

	int max_flow(int s, int t) {
		int flow = 0;
		this->t = t;
		while (1) {
			bfs(s);
			if (level[t] < 0) return flow;
			for (int i = 0; i < iter.size(); i++) iter[i] = 0;
			int f;
			while ((f = dfs(s, (1LL << 60))) > 0) {
				flow += f;
			}
		}
	}
};

signed main() {
	int H, W; cin >> H >> W;
	vector<string> a(H);
	REP (i, H) cin >> a[i];
	Graph g(H * W * 2);
	auto to_v = [&](int i, int j, bool is_first) {
		return H * W * (!is_first) + i * W + j;
	};
	II s, t;
	REP (i, H) {
		REP (j, W) {
			if (a[i][j] == '.') continue;
			if (a[i][j] == 'S') s = {i, j};
			if (a[i][j] == 'T') t = {i, j};
			g.add_edge(to_v(i, j, true), to_v(i, j, false), a[i][j] == 'o' ? 1 : 1e10);
			REP (i2, H) {
				if (i == i2 || a[i2][j] == '.') continue;
				g.add_edge(to_v(i, j, false), to_v(i2, j, true), 1e10);
			}
			REP (j2, W) {
				if (j == j2 || a[i][j2] == '.') continue;
				g.add_edge(to_v(i, j, false), to_v(i, j2, true), 1e10);
			}
		}
	}
	if (s.fi == t.fi || s.se == t.se) {
		cout << -1 << endl; return 0;
	}
	int ans = g.max_flow(to_v(s.fi, s.se, true), to_v(t.fi, t.se, false));
	cout << (ans >= 1e10 ? -1 : ans) << endl;
}

Measured memory_bytes: 124672