//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>

#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

#include <stdexcept>

using namespace std;

// type alias
using ll = long long;
using ull = unsigned long long;

//conversion
//------------------------------------------
inline int toint(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); }

//debug
#define DUMP(x)  cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (l" << __line__ << ")" << " " << __file__ << endl

inline ll floor(ll a, ull b)
{
	ll llb = b;
	if (a >= 0)
	{
		return a / llb;
	}
	else
	{
		return - ( (-a + llb - 1) / llb);
	}
}

inline ll ceil(ll a, ull b)
{
	ll llb = b;
	if (a >= 0)
	{
		return (a + llb - 1) / llb;
	}
	else
	{
		return - (-a / llb);
	}
}

inline ll getLowestOneBit(ll a)
{
	return a & (-a);
}

inline ll getHighestOneBit(ll a)
{
	while ((a & (a - 1)) != 0)
	{
		a = a & (a - 1);
	}
	return a;
}

inline bool isPower2(ull a)
{
	if (a == 0)
	{
		return false;
	}
	return !(a & (a - 1));
}

inline ll getSpecifiedBit(ll a, unsigned bit)
{
	return (a & (1LL << bit)) >> bit;
}

// computational complexity: o(log(max(a, b))) 
inline ull getGcd(ull a, ull b)
{
	if (b == 0)
	{
		return a;
	}
	return getGcd(b, a % b);
}

template<class Integer>
inline Integer getPower(Integer base, ull exponential)
{
	Integer result = 1;
	while (exponential >= 1)
	{
		if (exponential & 1)
		{
			result = result * base;
		}
		base = base * base;
		exponential >>= 1;
	}

	return result;
}

struct Edge
{
	ll to, capacity, reverse;
};

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	ll H, W;
	cin >> H >> W;

	ll nodeS = H + W;
	ll nodeT = H + W + 1;
	ll inf = H * W * 4;
	string line;
	vector<vector<Edge>> edges(H + W + 2);
	pair<ll, ll> posS, posT;
	for (ll i = 0; i < H; ++i)
	{
		cin >> line;
		for (ll j = H; j < H + W; ++j)
		{
			if (line[j - H] == 'o')
			{
				edges[i].push_back({ j, 1, ll(edges[j].size()) });
				edges[j].push_back({ i, 0, ll(edges[i].size()) - 1 });
				edges[j].push_back({ i, 1, ll(edges[i].size()) });
				edges[i].push_back({ j, 0, ll(edges[j].size()) - 1 });
			}
			else if (line[j - H] == 'S')
			{
				posS = {i, j - H};
				edges[nodeS].push_back({ i, inf, ll(edges[i].size()) });
				edges[i].push_back({ nodeS, 0, ll(edges[nodeS].size()) - 1 });
				edges[nodeS].push_back({ j, inf, ll(edges[j].size()) });
				edges[j].push_back({ nodeS, 0, ll(edges[nodeS].size()) - 1 });
			}
			else if (line[j - H] == 'T')
			{
				posT = { i, j - H };
				edges[i].push_back({ nodeT, inf, ll(edges[nodeT].size()) });
				edges[nodeT].push_back({ i, 0, ll(edges[i].size()) - 1 });
				edges[j].push_back({ nodeT, inf, ll(edges[nodeT].size()) });
				edges[nodeT].push_back({ j, 0, ll(edges[j].size()) - 1 });
			}
		}
	}

	if (posS.first == posT.first || posS.second == posT.second)
	{
		cout << -1 << endl;

		return 0;
	}

	struct Info
	{
		ll node, minCost;
	};

	ll res = 0;
	while (true)
	{
		stack<Info> infoStack;
		vector<pair<ll, ll>> isSelected(H + W + 2, { -1, -1 });
		infoStack.push({ nodeS, inf });
		isSelected[nodeS] = { nodeS, -1 };
		ll finalCost;
		bool canFindPath = false;
		while (!infoStack.empty())
		{
			Info info = infoStack.top();
			infoStack.pop();
			for (auto i = 0; i < edges[info.node].size(); ++i)
			{
				auto e = edges[info.node][i];
				if (e.capacity == 0 || isSelected[e.to].first != -1)
				{
					continue;
				}
				infoStack.push({ e.to, min(info.minCost, e.capacity) });
				isSelected[e.to] = { info.node, i };
				if (e.to == nodeT)
				{
					canFindPath = true;
					finalCost = info.minCost;
					goto LOOP_END;
				}
			}
		}
		break;
	LOOP_END:
		ll currNode = nodeT;
		while (currNode != isSelected[currNode].first)
		{
			auto& edge = edges[isSelected[currNode].first][isSelected[currNode].second];
			edge.capacity -= finalCost;
			edges[edge.to][edge.reverse].capacity += finalCost;
			currNode = isSelected[currNode].first;
		}
		res += finalCost;
	}

	cout << res << endl;

	return 0;
}


Measured memory_bytes: 1536