//#pragma warning(disable:4996)
//#include <Windows.h>
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
#include <complex>
#include <cstdio>
#include <list>
#include <bitset>
//#include <stdio.h>

//< in.txt > out.txt
using namespace std;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
//typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;

template<class T>
inline void chmin(T& a, T b) {
	a = min(a, b);
}

template<class T>
inline void chmax(T& a, T b) {
	a = max(a, b);
}

void input();
void solve();

void daminput();

int main() {
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	input();
	//daminput();
	solve();
	return 0;
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////

int H, W;
vector<string> A;

void input() {
	cin >> H >> W;
	A.resize(H);
	for (int y = 0; y < H; y++)cin >> A[y];
}

void daminput() {
}

template<class F>
class MaxFEdge {
public:
	int to;
	F cap;
	int rev;
	MaxFEdge(int _to, F _cap, int _rev) :to(_to), cap(_cap), rev(_rev) {}
};

template<class F>
class Dinic {
public:
	int V;
	vector<vector<MaxFEdge<F>>> G;
	VI itr, level;
	Dinic(int _v) :V(_v) {
		G.resize(V);
	}
	void AddEdge(int src, int to, F cap) {
		G[src].push_back(MaxFEdge<F>(to, cap, G[to].size()));
		G[to].push_back(MaxFEdge<F>(src, 0, G[src].size() - 1));
	}
	void bfs(int s) {
		level.assign(V, -1);
		queue<int> q;
		level[s] = 0;
		q.push(s);
		while (!q.empty()) {
			int v = q.front();
			q.pop();
			for (auto& e : G[v]) {
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					q.push(e.to);
				}
			}
		}
	}
	F dfs(int v, int t, F f) {
		if (v == t)return f;
		F res = 0;
		for (int& i = itr[v]; i < (int)G[v].size(); i++) {
			MaxFEdge<F>& e = G[v][i];
			if (e.cap > 0 && level[v] < level[e.to]) {
				F d = dfs(e.to, t, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					G[e.to][e.rev].cap += d;
					res += d;
					f -= d;
				}
			}
		}
		return res;
	}
	F MaxFlow(int s, int t) {
		F res = 0;
		F f = 0;
		F inf = numeric_limits<F>::max();
		while (true) {
			bfs(s);
			if (level[t] < 0)return res;
			itr.assign(V, 0);
			while (true) {
				f = dfs(s, t, inf);
				if (f <= 0)break;
				res += f;
			}
		}
	}
};

int conv(int y, int x) {
	return y * W + x;
}

PI S, T;

VVI M;   //1->マス有

void solve() {
	M.resize(H);
	for (int y = 0; y < H; y++) {
		M[y].resize(W);
		for (int x = 0; x < W; x++) {
			if (A[y][x] == 'S') {
				S = PI(y, x);
			}
			if (A[y][x] == 'T') {
				T = PI(y, x);
			}
			if (A[y][x] == '.') {
				M[y][x] = 0;
			}
			else {
				M[y][x] = 1;
			}
		}
	}
	if (S.first == T.first || S.second == T.second) {
		cout << "-1\n";
		return;
	}
	Dinic<LL> D(2 * H * W);
	for (int Y = 0; Y < H; Y++) {
		for (int X = 0; X < W; X++) {
			if (M[Y][X] == 0)continue;
			int C = conv(Y, X);
			D.AddEdge(2 * C, 2 * C + 1, 1);
			for (int y = 0; y < H; y++) {
				if (y == Y)continue;
				if (M[y][X] == 0)continue;
				int c = conv(y, X);
				D.AddEdge(2*C+1,2*c, 1e6);
			}
			for (int x = 0; x < W; x++) {
				if (x == X)continue;
				if (M[Y][x] == 0)continue;
				int c = conv(Y, x);
				D.AddEdge(2*C+1,2*c, 1e6);
			}
		}
	}
	int maxf = D.MaxFlow(2 * conv(S.first, S.second) + 1, 2 * conv(T.first, T.second));
	cout << maxf << "\n";
}

Measured memory_bytes: 127420