#include <bits/stdc++.h>
#include <chrono> 
 
using namespace std;
using namespace std::chrono; 
 
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
// #pragma optimization_level 3
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
#define f0r(a, b) for (long long a = 0; a < (b); ++a)
#define f1r(a, b, c) for (long long a = (b); a < (c); ++a)
#define f0rd(a, b) for (long long a = (b); a >= 0; --a)
#define f1rd(a, b, c) for (long long a = (b); a >= (c); --a)
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define pb push_back
#define send {ios_base::sync_with_stdio(false);}
#define help {cin.tie(NULL); cout.tie(NULL);}
#define fix(prec) {cout << setprecision(prec) << fixed;}
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define readgraph(list, edges) for (int i = 0; i < edges; i++) {int a, b; cin >> a >> b; a--; b--; list[a].pb(b); list[b].pb(a);}
#define ai(a, n) for (int ele = 0; ele < n; ele++) cin >> a[ele];
#define ain(a, lb, rb) for (int ele = lb; ele <= rb; ele++) cin >> a[ele];
#define ao(a, n) {for (int ele = 0; ele < (n); ele++) { if (ele) cout << " "; cout << a[ele]; } cout << '\n';}
#define aout(a, lb, rb) {for (int ele = (lb); ele <= (rb); ele++) { if (ele > (lb)) cout << " "; cout << a[ele]; } cout << '\n';}
#define vsz(x) ((long long) x.size())
typedef long long ll;
typedef double ld;
typedef long double lld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
 
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
  cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
  cin >> p.first;
  return cin >> p.second;
}
 
// mt19937 rng(steady_clock::now().time_since_epoch().count());
mt19937 rng(61378913);
/* usage - just do rng() */
 
void usaco(string filename) {
  // #pragma message("be careful, freopen may be wrong")
	freopen((filename + ".in").c_str(), "r", stdin);
	freopen((filename + ".out").c_str(), "w", stdout);
}
 
const lld pi = 3.14159265358979323846;
const ll mod = 1000000007;
// const ll mod = 998244353;
// ll mod;


 
ll n, m, k, q, l, r, x, y, z;
ll a[1000005];
ll b[1000005];
ll c[1000005];
string s, t;
ll ans = 0;

// benq
#define sz(x) ((int)x.size())

template<int SZ> struct Dinic {
	int N,s,t; // # verts, source, sink
	typedef ll F; // flow type
	struct Edge { int to, rev; F flo, cap; };
	vector<Edge> adj[SZ]; // use asserts, don't be dumb
	void ae(int u, int v, F cap, F rcap = 0) { 
		assert(min(cap,rcap) >= 0); 
		Edge a{v,sz(adj[v]),0,cap}, b{u,sz(adj[u]),0,rcap};
		adj[u].pb(a), adj[v].pb(b); } 
	int lev[SZ]; typename vector<Edge>::iterator cur[SZ]; 
	bool bfs() { // level = shortest distance from source
		f0r(i,N) lev[i] = -1, cur[i] = begin(adj[i]);
		queue<int> q({s}); lev[s] = 0; 
		while (sz(q)) {
			int u = q.front(); q.pop();
			for(auto e: adj[u]) if (lev[e.to] < 0 && e.flo < e.cap) 
				q.push(e.to), lev[e.to] = lev[u]+1;
		}
		return lev[t] >= 0;
	}
	F dfs(int v, F flo) {
		if (v == t) return flo;
		for (; cur[v] != end(adj[v]); cur[v]++) {
			Edge& e = *cur[v];
			if (lev[e.to]!=lev[v]+1||e.flo==e.cap) continue;
			F df = dfs(e.to,min(flo,e.cap-e.flo));
			if (df) { e.flo += df; adj[e.to][e.rev].flo -= df;
				return df; } // saturated >=1 one edge
		}
		return 0;
	}
	F maxFlow(int _N, int _s, int _t) {
		N = _N, s = _s, t = _t; assert(s != t);
		F tot = 0; while (bfs()) while (F df = 
			dfs(s,numeric_limits<F>::max())) tot += df;
		return tot;
	}
};

char mat[105][105];
int best[105][105];
vpi par[105][105];
Dinic<20005> din;

int id(int r, int c) {
  return r * m + c;
}

int vin(int v) { return 2 * v; }
int vout (int v) { return 2 * v + 1; }

void solve(int tc) {
  cin >> n >> m;

  pii s, t;
  f0r(i, n) {
    string z; cin >> z;
    f0r(j, m) {
      mat[i][j] = z[j];
      if (mat[i][j] == 'S') {
        s = mp(i, j);
      } else if (mat[i][j] == 'T') {
        t = mp(i, j);
      }
    }
  }

  if (s.f == t.f || s.s == t.s) {
    cout << -1 << '\n';
    return;
  }

  f0r(i, n) f0r(j, m) best[i][j] = mod;

  queue<pii> q;
  q.push(s);
  best[s.f][s.s] = 0;
  while (!q.empty()) {
    pii x = q.front();
    q.pop();

    int nv = best[x.f][x.s] + 1;
    f0r(i, n) {
      if (i != x.f && mat[i][x.s] != '.') {
        par[i][x.s].pb(x);
        if (best[i][x.s] >= nv) {
          if (best[i][x.s] > nv) q.push(mp(i, x.s));
          best[i][x.s] = nv;
        }
      }
    }
    f0r(i, m) {
      if (i != x.s && mat[x.f][i] != '.') {
        par[x.f][i].pb(x);
        if (best[x.f][i] >= nv) {
          if (best[x.f][i] > nv) q.push(mp(x.f, i));
          best[x.f][i] = nv;
        }
      }
    }
  }

  f0r(i, n) f0r(j, m) {
    int cur = id(i, j);
    // cout << i << " " << j << " " << par[i][j] << endl;
    if (mat[i][j] != '.') {
      for (pii x: par[i][j]) {
        int last = id(x.f, x.s);
        // cout << vout(last) << " " << vin(cur) << endl;
        din.ae(vout(last), vin(cur), mod);
      }
    }
    din.ae(vin(cur), vout(cur), 1);
  }

  cout << din.maxFlow(n * m * 2, vout(id(s.f, s.s)), vin(id(t.f, t.s))) << '\n';
}
 
int main() {
  send help
 
  // usaco("cowland");

  int tc = 1;
  // cin >> tc;
  for (int t = 0; t < tc; t++) solve(t);
} 

Measured memory_bytes: 148108