#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

template<class T> class Dinic {
	struct edge {
		int to;
		T cap;
		int rev;
	};
	vector<vector<edge>> graph;
	vector<int> level, iter;
public:
	Dinic(int n) {
		graph = vector<vector<edge>>(n, vector<edge>());
		level = iter = vector<int>(n);
	}
	void add_edge(int from, int to, T cap) {
		graph[from].push_back({ to,cap,(int)graph[to].size() });
		graph[to].push_back({ from,0,(int)graph[from].size() - 1 });
	}
	void bfs(int s) {
		for (int &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 < graph[v].size(); i++) {
				edge &e = graph[v][i];
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					que.push(e.to);
				}
			}
		}
	}
	T dfs(int v, int t, T f) {
		if (v == t) return f;
		for (int &i = iter[v]; i < graph[v].size(); i++) {
			edge &e = graph[v][i];
			if (e.cap > 0 && level[v] < level[e.to]) {
				T d = dfs(e.to, t, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					graph[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}
	T max_flow(int s, int t) {
		T flow = 0;
		while (true) {
			bfs(s);
			if (level[t] < 0) return flow;
			for (int &i : iter) i = 0;
			T f = 0;
			while ((f = dfs(s, t, inf)) > 0) flow += f;
		}
	}
	void debug() {
		for (int i = 0; i < graph.size(); i++) {
			for (auto &e : graph[i]) {
				if (e.cap) {
					cerr << i << ' ' << e.to << ' ' << e.cap << '\n';
				}
			}
		}
	}
};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);

	int h, w;
	cin >> h >> w;
	vector<vector<char>> a(h, vector<char>(w));
	rep(i, h) rep(j, w) cin >> a[i][j];
	Dinic<int> flow(h + w + 2);
	rep(i, h) {
		rep(j, w) {
			if (a[i][j] == 'S') {
				flow.add_edge(h + w, i, inf);
				flow.add_edge(h + w, h + j, inf);
			}
			else if (a[i][j] == 'T') {
				flow.add_edge(i, h + w + 1, inf);
				flow.add_edge(h + j, h + w + 1, inf);
			}
			else if (a[i][j] == 'o') {
				flow.add_edge(i, h + j, 1);
				flow.add_edge(h + j, i, 1);
			}
		}
	}
	int f = flow.max_flow(h + w, h + w + 1);
	cout << (f < inf ? f : -1) << '\n';
}

Measured memory_bytes: 896