#include <iostream>
#include<bits/stdc++.h>
#define INF 100000
using namespace std;
struct edge {int to,w,p;};
int h,w,used[202],a,b,c,cnt,sx,sy,tx,ty;
vector<edge> v[202];
int dfs(int s,int t,int r){
    if(s==t)return r;
    used[s]=1;
    for(int i=0;i<v[s].size();i++){
        edge &u=v[s][i];
        if(used[u.to]==0&&u.w>0){
            int F=dfs(u.to,t,min(r,u.w));
            if(F>0){
                u.w-=F;v[u.to][u.p].w+=F;return F;
            }
        }
    }
    return 0;
}
string s[100];
int main(void){
    cin>>h>>w;
    for(int i=0;i<h;i++){
        cin>>s[i];
        for(int j=0;j<w;j++){
            if(s[i][j]=='S'){
                sx=i,sy=j;
                a=200,b=i;
                edge p={b,INF,(int)v[b].size()};v[a].push_back(p);
                edge q={a,0,(int)v[a].size()-1};v[b].push_back(q);
                a=200,b=j+100;
                p={b,INF,(int)v[b].size()};v[a].push_back(p);
                q={a,0,(int)v[a].size()-1};v[b].push_back(q);
            }
            if(s[i][j]=='T'){
                tx=i,ty=j;
                a=i,b=201;
                edge p={b,INF,(int)v[b].size()};v[a].push_back(p);
                edge q={a,0,(int)v[a].size()-1};v[b].push_back(q);
                a=j+100,b=201;
                p={b,INF,(int)v[b].size()};v[a].push_back(p);
                q={a,0,(int)v[a].size()-1};v[b].push_back(q);
            }
            if(s[i][j]=='o'){
                a=i,b=100+j;
                edge p={b,1,(int)v[b].size()};v[a].push_back(p);
                edge q={a,0,(int)v[a].size()-1};v[b].push_back(q);
                a=100+j,b=i;
                p={b,1,(int)v[b].size()};v[a].push_back(p);
                q={a,0,(int)v[a].size()-1};v[b].push_back(q);
            }
        }
    }
    if(sx==tx||sy==ty){
        cout<<-1<<endl;
        return 0;
    }
     while(1){
        for(int i=0;i<202;i++)used[i]=0;
        int P=dfs(200,201,1e9);
        if(P==0)break;
        cnt+=P;
    }
    cout<<cnt<<endl;
}
    /*
    for(int k=0;k<h;k++){
        if(k!=sx&&s[k][sy]!='.'){
            if(s[k][sy]=='T'){cout<<-1<<endl;return 0;}
            a=200;
            b=
            edge p={b,1,(int)v[b].size()};v[a].push_back(p);
            edge q={a,0,(int)v[a].size()-1};v[b].push_back(q);
        }
    }
    for(int k=0;k<w;k++){
        if(k!=sy&&s[sx][k]!='.'){
            if(s[sx][k]=='T'){cout<<-1<<endl;return 0;}
            a=10*sx+sy;
            b=10*sx+k;
            edge p={b,INF,(int)v[b].size()};v[a].push_back(p);
            edge q={a,0,(int)v[a].size()-1};v[b].push_back(q);
        }
    }
    for(int k=0;k<h;k++){
        if(k!=tx&&s[k][ty]!='.'){
            if(s[k][ty]=='S'){cout<<-1<<endl;return 0;}
            a=10*tx+ty;
            b=10*k+ty;
            edge p={b,1,(int)v[b].size()};v[a].push_back(p);
            edge q={a,0,(int)v[a].size()-1};v[b].push_back(q);
        }
    }
    for(int k=0;k<w;k++){
        if(k!=ty&&s[tx][k]!='.'){
            if(s[tx][k]=='S'){cout<<-1<<endl;return 0;}
            a=10*tx+ty;
            b=10*tx+k;
            edge p={b,1,(int)v[b].size()};v[a].push_back(p);
            edge q={a,0,(int)v[a].size()-1};v[b].push_back(q);
        }
    }
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(s[i][j]=='o'){
                for(int k=0;k<h;k++){
                    if(i!=k&&s[k][j]!='.'&&s[k][j]!='S'){
                        a=10*i+j;
                        b=10*k+j;
                        int L=1;
                        if(s[k][j]=='T')L=INF;
                        edge p={b,L,(int)v[b].size()};v[a].push_back(p);
                        edge q={a,0,(int)v[a].size()-1};v[b].push_back(q);
                    }
                }
                for(int k=0;k<w;k++){
                    if(j!=k&&s[i][k]!='.'&&s[i][k]!='S'){
                        a=10*i+j;
                        b=10*i+k;
                        int L=1;
                        if(s[i][k]=='T')L=INF;
                        edge p={b,L,(int)v[b].size()};v[a].push_back(p);
                        edge q={a,0,(int)v[a].size()-1};v[b].push_back(q);
                    }
                }
            }
        }
    }
    for(int i=0;i<m;i++){
        cin>>a>>b>>c;
        edge p={b,c,(int)v[b].size()};v[a].push_back(p);
        edge q={a,0,(int)v[a].size()-1};v[b].push_back(q);
    }*/
    //int res=0;
   


Measured memory_bytes: 896