#include<iostream>
#include<cstring> 
#include<cstdio>
#define S 0
#define T 201
#define INF 2000000000
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
char s[T+5][T+5];
int px,py,Px,Py,n,m,head[T+5],cnt=1,q[T+5],top,d[T+5],ans,c[T+5];
struct edge{int to,next,w;}e[T*T*10+5];
inline void ins(int f,int t,int w)
{
	e[++cnt]=(edge){t,head[f],w};head[f]=cnt;
	e[++cnt]=(edge){f,head[t],0};head[t]=cnt; 
}
bool bfs()
{
	memset(d,0,sizeof(d));int i,j;
	for(d[q[top=i=1]=S]=1;i<=top;++i)
		for(j=c[q[i]]=head[q[i]];j;j=e[j].next)
			if(e[j].w&&!d[e[j].to]) d[q[++top]=e[j].to]=d[q[i]]+1;
	return d[T];
}
int dfs(int x,int f)
{
	if(x==T) return f;int used=0;
	for(int&i=c[x];i;i=e[i].next)
		if(e[i].w&&d[e[i].to]==d[x]+1)
		{
			int w=dfs(e[i].to,min(f-used,e[i].w));
			used+=w;e[i].w-=w;e[i^1].w+=w;
			if(used==f) return f;
		}
	return d[x]=-1,used;
}
int main()
{
	n=read();m=read();
	for(int i=1;i<=n;++i) scanf("%s",s[i]+1);
	for(int i=1;i<=n;++i) for(int j=1;j<=m;++j)
	{
		if(s[i][j]!='.') ins(i,j+n,1),ins(j+n,i,1);
		if(s[i][j]=='S') px=i,py=j,ins(S,i,INF),ins(S,j+n,INF);
		if(s[i][j]=='T') Px=i,Py=j,ins(i,T,INF),ins(j+n,T,INF);
	}
	if(Px==px||Py==py) return 0*puts("-1");
	while(bfs()) ans+=dfs(S,INF);
	printf("%d",ans);
	return 0;
}

