#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=1000000000;
struct edge{
	int t,next,v,rev;
}g[1100001];
char ch[501];
int s,t,tot,ans,i,j,k,n,m,h[41001],dis[41001],q[41001];
void addedge(int x,int y,int z){
	g[++tot].t=y;g[tot].next=h[x];h[x]=tot;g[tot].v=z;g[tot].rev=tot+1;
	g[++tot].t=x;g[tot].next=h[y];h[y]=tot;g[tot].v=0;g[tot].rev=tot-1;
}
bool bfs(){
    int l,r;
    memset(dis,0,sizeof(dis));
    for (l=0,r=1,q[1]=s,dis[s]=1;l<r;){
        int j=q[++l];
        for (int i=h[j];i;i=g[i].next)
         if (g[i].v&&!dis[g[i].t]){
            dis[g[i].t]=dis[j]+1;q[++r]=g[i].t;
            if (t==g[i].t) return 1;
         }
    }
    return 0;
}
int work(int x,int low){
    if (x==t) return low;
    int res=0;
    for (int i=h[x];i&&low;i=g[i].next)
     if (g[i].v&&dis[g[i].t]==dis[x]+1){
        int f=work(g[i].t,min(low,g[i].v));
        res+=f;g[g[i].rev].v+=f;
        g[i].v-=f;low-=f;
     }
    return res;
}
int main(){
	scanf("%d%d",&n,&m);s=2*n*m+n+m+1;t=s+1;tot=0;memset(h,0,sizeof(h));
	for (i=1,k=0;i<=n;i++){
		scanf("%s",ch);
		for (j=1;j<=m;j++){
			k++;
			if (ch[j-1]=='.') continue;
			if (ch[j-1]=='S') addedge(s,2*k-1,inf);
			if (ch[j-1]=='T') addedge(2*k,t,inf);
			addedge(2*k-1,2*k,(ch[j-1]=='o')?1:inf);
			addedge(2*k,2*n*m+i,inf);addedge(2*n*m+i,2*k-1,inf);
			addedge(2*k,2*n*m+n+j,inf);addedge(2*n*m+n+j,2*k-1,inf);
		}
	}
	for (ans=0;bfs();ans+=work(s,inf));
	printf("%d\n",ans==inf?-1:ans);
	return 0;
}