#include<bits/stdc++.h>
using namespace std;
#define y1 A2Dalek
#define y0 Cyberman
#define yn RoseTyler
#define j1 JackHarkness
#define fs first
#define sd second
const int N=300;
const int oo=1e9+5;
int du[]={-1,0,0,1};
int dv[]={0,-1,1,0};
const long long mod=1e9+7;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
long long sqr(long long x) {return x*x;}
int getbit(int state, int i) {return ((state>>i)&1);}

int n,m,res,t,vis[N+5],f[N+5][N+5],c[N+5][N+5],d[N+5];
vector<int> g[N+5];
bool bfs()
{
    for (int i=0;i<=n+m+1;i++) d[i]=0;
    d[0]=1;
    queue<int> q;
    q.push(0);
    while (!q.empty())
    {
        int u=q.front(); q.pop();
        if (u==n+m+1) return true;
        for (int i=0;i<g[u].size();i++)
        {
            int v=g[u][i];
            if (d[v] || f[u][v]>=c[u][v]) continue;
            d[v]=d[u]+1;
            q.push(v);
        }
    }
    return false;
}
int dfs(int u, int Min)
{
    if (u==n+m+1) return Min;
    if (vis[u]!=t) vis[u]=t; else return 0;
    for (int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if (f[u][v]<c[u][v] && vis[v]!=t && d[v]==d[u]+1)
        {
            if (int x=dfs(v,min(Min,c[u][v]-f[u][v])))
            {
                f[u][v]+=x;
                f[v][u]-=x;
                return x;
            }
        }
    }
    return 0;
}
int main()
{
	//freopen("Splay Tree.inp","r",stdin);
	//freopen("Splay Tree.out","w",stdout);
	ios_base::sync_with_stdio(false);
    cin>>n>>m;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++)
    {
        char cs;
        cin>>cs;
        if (cs=='S')
        {
            g[0].push_back(i);
            g[0].push_back(j+n);
            g[j+n].push_back(0);
            g[i].push_back(0);
            c[0][i]=oo; c[0][j+n]=oo;
        } else if (cs=='T')
        {
            g[i].push_back(n+m+1);
            g[j+n].push_back(n+m+1);
            g[n+m+1].push_back(i);
            g[n+m+1].push_back(j+n);
            c[i][n+m+1]=oo; c[j+n][n+m+1]=oo;
        } else if (cs=='o')
        {
            g[i].push_back(j+n);
            g[j+n].push_back(i);
            c[i][j+n]=c[j+n][i]=1;
        }
    }
    while (bfs())
    {
        while (int x=(++t,dfs(0,oo))) res+=x;
    }
    if (res>=oo) cout<<-1; else cout<<res;
	return 0;
}
