// Original C++ solution (slower):
#include<bits/stdc++.h>

#define LL long long

#define MEM(x,y) memset(x,y,sizeof(x))

#define MOD(x) ((x)%mod)

#define mod 1000000007

#define pb push_back

#define STREAM_FAST ios::sync_with_stdio(false)

 

using namespace std;

const int maxn = 2e5+7;

struct node {int v,flow,nxt;}e[maxn];

int tot=1,head[maxn],dep[maxn];

// tot starting from 1

void add(int u,int v,int w)

{

    ++tot;

    e[tot].v=v;e[tot].flow=w;e[tot].nxt=head[u];

    head[u]=tot;

}

 

bool bfs(int S,int T)

{

    MEM(dep,0);

    dep[S]=1;

    queue<int>q;

    q.push(S);

    while (!q.empty())

    {

        int u=q.front();q.pop();

        for (int i=head[u];i;i=e[i].nxt)

        {

            int v=e[i].v;

            if (e[i].flow && !dep[v])

            {

                dep[v]=dep[u]+1;

                q.push(e[i].v);

            }

        }

    }

    return dep[T];

}

 

int aim;

int dfs(int now,int flow)

{

    if (now==aim) return flow;

    int f=0;

    for (int i=head[now];i;i=e[i].nxt)

    {

        int v=e[i].v;

        if (e[i].flow && dep[v]==dep[now]+1)

        {

            int x=dfs(v,min(flow,e[i].flow));

            e[i].flow-=x;e[i^1].flow+=x;flow-=x;f+=x;

        }

    }

    if (!f) dep[now]=-2;

    return f;

}

 

int maxflow(int S,int T)

{

    aim=T;int res=0;

    while (bfs(S,T)) res+=dfs(S,1<<30);

    return res;

}

 

int main()

{

    int n,m;scanf("%d%d",&n,&m);

    int S=1000,T=1001;

    

    for (int i=1;i<=n;i++)

    {

        for (int j=1;j<=m;j++)

        {

            char c;scanf(" %c",&c);

            if (c=='o') 

            {

                add(i,j+n,1);add(j+n,i,1);

            }

            else if (c=='S')

            {

                add(S,i,1e8);add(i,S,0);

                add(S,j+n,1e8);add(j+n,S,0);

            }

            else if (c=='T')

            {

                add(i,T,1e8);add(T,i,0);

                add(j+n,T,1e8);add(T,j+n,0);

            }

        }

    }

    

    int ans=maxflow(S,T);

    printf("%d\n",ans>=1e8?-1:ans);

    

    return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef unsigned long long ull;

#define int long long

int hmt() {int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';if(n) x=-x;return x;}

#define in hmt()

#define ins ({string x;char c=getchar();for(;c==' '||c=='\n';c=getchar());for(;c!=' '&&c!='\n';c=getchar()) x+=c;x;})

#define forinc(i,a,b) for(int i=a,_b=b;i<=_b;++i)

#define fordec(i,a,b) for(int i=a;i>=b;--i)

#define forb(i,BS) for(int i=BS._Find_first();i< BS.size();i = BS._Find_next(i))

#define forv(a,b) for(auto &a:b)

#define pb push_back

#define pii pair<int,int>

#define fi first

#define se second

#define all(a) a.begin(),a.end()

#define reset(f,x) memset(f,x,sizeof(f))

#define bit(x,i) ((x>>(i-1))&1)

#define onbit(x,i) (x|(1<<(i-1)))

#define offbit(x,i) (x&~(1<<(i-1)))

using namespace std;

const int N=205,M=40005;

int ft[N],cu[N],q[N],h,t,tot=1,ans,S,T,d[N];

struct edge

{

    int t,f,n;

} E[M];

void add(int x,int y,int f)

{

    E[++tot]=(edge) {y,f,ft[x]},ft[x]=tot;

    E[++tot]=(edge) {x,0,ft[y]},ft[y]=tot;

}

#define FOR for (int o=cu[u],v;v=E[o].t;o=E[o].n)

void bfs(int u=S)

{

    memset(d,0,sizeof(d));

    memcpy(cu,ft,sizeof(cu));

    for (d[q[h=t=1]=u]=1; h<=t; u=q[++h])

        FOR if (E[o].f&&!d[v])

            d[q[++t]=v]=d[u]+1;

}

int dfs(int u,int f)

{

    if (u==T||!f)

        return f;

    int g=0,t;

    FOR if (cu[u]=o,d[v]==d[u]+1&&(t=dfs(v,min(f,E[o].f))))

        g+=t,f-=t,E[o].f-=t,E[o^1].f+=t;

    return g;

}

int n,m;

main()

{

    scanf("%d%d",&n,&m);

    forinc(i,1,n) forinc(j,1,m)

    {

        char c;

        cin>>c;

        if (c=='.') continue;

        int f=1;

        if (c=='S') S=i,f=1000;

        if (c=='T') T=i,f=1000;

        add(i,j+n,f);

        add(j+n,i,f);

    }

    while (bfs(),d[T]&&ans<1e7) ans+=dfs(S,1e8);

    printf("%d\n",ans>n+m?-1:ans);

}


// Fastest known solution for this problem: