#include <bits/stdc++.h>

using namespace std;
string S[101];
int s , t ;
int dx[]={1 , 0  , -1 , 0 };
int dy[]={0  , 1 , 0 , -1 };
int g[100][100];

const int N = 200024, E = 4000000;    ///N must be a power of 2
int nbr;
int head[N],to[E],nxt[E],cap[E];
int e;
void init()
{
    e=0;
    memset(head,-1,sizeof head);
}
void addEdge(int f, int t, int w)
{
    nxt[e]=head[f];
    head[f]=e;
    cap[e]=w;
    to[e++]=t;
}
void addBiEdge(int f,int t,int w)
{
    addEdge(f,t,w);
    addEdge(t,f,0);
}
pair<int,int> mp[10001];

typedef int LL;
int vis[N],ID;
int fromid[N];
int fromnode[N];
int MM;
LL bfs(int src,int snk) {

    queue<int> q ;
    q.push(src);
    vis[src]=ID;
    while(!q.empty()){
        MM=max(MM,(int)q.size());
        int cur=q.front();
        q.pop();
        if(cur==snk){
            while(cur!=src){
                cap[fromid[cur]]-=1;
                cap[fromid[cur]^1]+=1;
                cur=fromnode[cur];
            }
            return 1;
        }
        for (int i = head[cur]; i != -1; i = nxt[i]) {
            int t = to[i];
            if(cap[i]>0 &&vis[t]!=ID){
                fromid[t]=i;
                fromnode[t]=cur;
                vis[t]=ID;
                q.push(t);
            }
        }
    }
    return 0;
}
// +201278474570 tamer :)

LL dfs(int cur,int snk, LL MX) {
//    cout << cur <<' ' ;
    if (vis[cur] == ID || MX == 0)
        return 0;
    vis[cur] = ID;
    if (cur == snk)
        return MX;
    for (int i = head[cur]; i != -1; i = nxt[i]) {
        int t = to[i];
        LL f = dfs(t,snk, min(MX, cap[i]));
        if (!f)
            continue;
        cap[i] -= f;
        cap[i ^ 1] += f;
        return f;
    }

    return 0;
}

int out(int i){
    return i*2+1;
}

int in(int i){
    return i*2;
}

int maxFlow(int src,int snk) {
    int ret = 0;
    if (src == snk)
        return INT_MAX;
    int f;
    for (ID++; (f = bfs(out(src),in(snk))), f; ID++)
        ret += f ;
    return ret;
}
int n , m ;

bool get(int x , int y){
    return (x>=0 && x<n && y>=0 &&y<m);
}

int main()
{

    cin >> n >> m ;
//    n = m = 100;
    for(int i = 0 ; i< n ; i++){
        cin >> S[i];
//
//        for(int j = 0 ; j<m ; j++){
//            if(i==34 &&j==45)S[i]+='S';
//            else if(i==4&&j==9)S[i]+='T';
//            else S[i]+='o';
//        }
    }
    int nbr = 0;
    for(int i = 0 ; i< n ; i++){
        for(int j = 0 ; j< m ; j++){
            if(S[i][j]=='.')continue;
            g[i][j]= ++nbr;
            mp[nbr]=make_pair(i,j);
            if(S[i][j]=='S')s=nbr;
            if(S[i][j]=='T')t=nbr;
        }
    }
    init();
    bool ANS=1;
    for(int i = 1 ; i<= nbr ; i++){
        int x = mp[i].first ;
        int y = mp[i].second ;
        addBiEdge(in(i),out(i),1);
//        cout << x << ' ' << y <<endl;
        for(int k = 0 ; k< 4 ; k++){
            int xx= x ;
            int yy= y ;
            while(1){
                xx+= dx[k];
                yy+= dy[k];
//                cout << xx << ' ' << yy <<endl;
                if(!get(xx,yy))break;
                if(g[xx][yy]){
                    addBiEdge(out(i),in(g[xx][yy]),1<<10);
                    if(i==s && g[xx][yy]==t)ANS =0 ;
                    if(i==t && g[xx][yy]==s)ANS =0 ;
                }
            }
        }
    }
    if(!ANS)cout<<-1 ;
    else if(nbr==n*m) cout <<n+m-2;
    else cout<<maxFlow(s,t);
    return 0;
}


Measured memory_bytes: 48256