#include <bits/stdc++.h>
using namespace std;
struct pt { int x,y,ch; };
pt tr[101][101][2];
struct edge { int k,ch; };
edge tre[101][101][2];
vector <pt> a[101][101][2];
vector <edge> e[101][101][2];
int f[10001][2],c[10001][2],i,j,k,n,m,kq,xs,ys,xt,yt,cnt;
char b[101][101];
const int oo=round(1e9);
bool BFS()
{
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++) tr[i][j][0]=tr[i][j][1]={ 0,0,0 };
    queue <pt> q;
    q.push({ xs,ys,0 }); tr[xs][ys][0]={ xs,ys,0 };
    while(q.size()>0)
    {
        int x=q.front().x,y=q.front().y,ch=q.front().ch; q.pop();
        if(x==xt && y==yt && ch==1) return 1;
        for(i=0;i<a[x][y][ch].size();i++)
        {
            int x2=a[x][y][ch][i].x,y2=a[x][y][ch][i].y,ch2=a[x][y][ch][i].ch;
            edge tg=e[x][y][ch][i];
            if(tr[x2][y2][ch2].x==0 && f[tg.k][tg.ch]<c[tg.k][tg.ch])
            {
                q.push({ x2,y2,ch2 });
                tr[x2][y2][ch2]={ x,y,ch }; tre[x2][y2][ch2]=tg;
            }
        }
    }
    return 0;
}
void incflow()
{
    kq++;
    int x=xt,y=yt,ch=1;
    while(x!=xs || y!=ys || ch!=0)
    {
        int x2=tr[x][y][ch].x,y2=tr[x][y][ch].y,ch2=tr[x][y][ch].ch;
        edge tg=tre[x][y][ch];
        f[tg.k][tg.ch]++; f[tg.k][1-tg.ch]--;
        x=x2; y=y2; ch=ch2;
    }
}
void add(int xu,int yu,int chu,int xv,int yv,int chv,int k)
{
    cnt++;
    a[xu][yu][chu].push_back({ xv,yv,chv }); e[xu][yu][chu].push_back({ cnt,0 });
    a[xv][yv][chv].push_back({ xu,yu,chu }); e[xv][yv][chv].push_back({ cnt,1 });
    c[cnt][0]=k;
}
int main()
{
  //  freopen("ntu.inp","r",stdin);
   // freopen("ntu.out","w",stdout);
    cin>>n>>m;
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
        {
            cin>>b[i][j];
            if(b[i][j]=='S') { xs=i; ys=j; }
            if(b[i][j]=='T') { xt=i; yt=j; }
        }
    if(xs==xt || ys==yt) { cout<<-1; return 0; }
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
        {
            if(b[i][j]=='.') continue;
            if(b[i][j]=='o') add(i,j,0,i,j,1,1); else add(i,j,0,i,j,1,oo);
            for(k=1;k<=n;k++)
                if(k!=i && b[k][j]!='.') add(i,j,1,k,j,0,oo);
            for(k=1;k<=m;k++)
                if(k!=j && b[i][k]!='.') add(i,j,1,i,k,0,oo);
        }
    while(BFS()) incflow();
    cout<<kq;
}
