// Adjacency list implementation of Dinic's blocking flow algorithm.
// This is very fast in practice, and only loses to push-relabel flow.
//
// Running time:
//     O(|V|^2 |E|)
//
// INPUT:
//     - graph, constructed using AddEdge()
//     - source and sink
//
// OUTPUT:
//     - maximum flow value
//     - To obtain actual flow values, look at edges with capacity > 0
//       (zero capacity edges are residual edges).

#include <cstdio>
#include <vector>
#include <queue>
#include <bits/stdc++.h>
#define MAX 1000005
#define ll long long
#define upperlimit 1000100
#define INF 1e18
#define eps 1e-8
#define endl '\n'
#define pcc pair<char,char>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define tr(container,it) for(typeof(container.begin()) it=container.begin();it!=container.end();it++)
#define MOD 1000000007
#define slld(t) scanf("%lld",&t)
#define sd(t) scanf("%d",&t)
#define pd(t) printf("%d\n",t)
#define plld(t) printf("%lld\n",t)
#define mp(a,b) make_pair(a,b)
#define FF first
#define SS second
#define pb(x) push_back(x)
#define vi vector<int>
#define vll vector<ll>
#define clr(a) memset(a,0,sizeof(a))
#define debug(a) printf("check%d\n",a)
#define csl ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long LL;

struct Edge {
  int u, v;
  LL cap, flow;
  Edge() {}
  Edge(int u, int v, LL cap): u(u), v(v), cap(cap), flow(0) {}
};
int cnt=0;
struct Dinic {
  int N;
  vector<Edge> E;
  vector<vector<int>> g;
  vector<int> d, pt;
  
  Dinic(int N): N(N), E(0), g(N), d(N), pt(N) {}

  void AddEdge(int u, int v, LL cap) {
  	cnt++;
    if (u != v) {
      E.emplace_back(Edge(u, v, cap));
      g[u].emplace_back(E.size() - 1);
      E.emplace_back(Edge(v, u, 0));
      g[v].emplace_back(E.size() - 1);
    }
  }

  bool BFS(int S, int T) {
    queue<int> q({S});
    fill(d.begin(), d.end(), N + 1);
    d[S] = 0;
    while(!q.empty()) {
      int u = q.front(); q.pop();
      if (u == T) break;
      for (int k: g[u]) {
        Edge &e = E[k];
        if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {
          d[e.v] = d[e.u] + 1;
          q.emplace(e.v);
        }
      }
    }
    return d[T] != N + 1;
  }

  LL DFS(int u, int T, LL flow = -1) {
    if (u == T || flow == 0) return flow;
    for (int &i = pt[u]; i < g[u].size(); ++i) {
      Edge &e = E[g[u][i]];
      Edge &oe = E[g[u][i]^1];
      if (d[e.v] == d[e.u] + 1) {
        LL amt = e.cap - e.flow;
        if (flow != -1 && amt > flow) amt = flow;
        if (LL pushed = DFS(e.v, T, amt)) {
          e.flow += pushed;
          oe.flow -= pushed;
          return pushed;
        }
      }
    }
    return 0;
  }

  LL MaxFlow(int S, int T) {
    LL total = 0;
    while (BFS(S, T)) {
      fill(pt.begin(), pt.end(), 0);
      while (LL flow = DFS(S, T))
        total += flow;
    }
    return total;
  }
};

// BEGIN CUT
// The following code solves SPOJ problem #4110: Fast Maximum Flow (FASTFLOW)
char arr[200][200];
int main()
{
	int x,y,str=0,end=0;
	cin>>x>>y;
	for(int i=0;i<x;i++)
	{
		for(int j=0;j<y;j++)
		{
			cin>>arr[i][j];
		}
	}
	Dinic dinic(2*x*y);
	for(int i=0;i<x;i++)
	{
		for(int j=0;j<y;j++)
		{
			dinic.AddEdge(j*x+i,j*x+i+x*y,1);
			//dinic.AddEdge(j*x+i+x*y,j*x+i,1);
			if(arr[i][j]!='.')
			{
				for(int k=0;k<y;k++)
				{
					if(k!=j)
					{
						if(arr[i][k]!='.')
						{
							dinic.AddEdge(x*y+j*x+i,k*x+i,100);
						}

					}
				}
				for(int k=0;k<x;k++)
				{
					if(k!=i)
					{
						if(arr[k][j]!='.')
						{
							dinic.AddEdge(x*y+j*x+i,j*x+k,100);
						}
					}
				}
			}
			if(arr[i][j]=='S')
				str=j*x+i;
			if(arr[i][j]=='T')
				end=j*x+i;
		}
	}
	if(str%x==end%x)
		dinic.AddEdge(str+x*y,end,MOD);
	else if(str/x==end/x)
		dinic.AddEdge(str+x*y,end,MOD);
	int ans=dinic.MaxFlow(x*y+str,end);
	if(ans<1000000)
	cout<<ans<<endl;
else cout<<-1<<endl;
	//cout<<cnt<<endl;
  return 0;
}

// END CUT

Measured memory_bytes: 116112