#include<cstdio>
#include<vector>
using std::vector;
const int inf = 1e8+7;
struct Edge{
    int u,v;
    int c;
    int rev;
    Edge(int u,int v,int c,int rev):u(u),v(v),c(c),rev(rev){}
};
char s[105][105];
int depth[30005]; 
vector<Edge>g[30005];
int queue[30005];

void addEdge(int u,int v,int c){
    //printf("u = %d, v = %d, c = %d\n",u,v,c);
    int ur = g[u].size(), vr = g[v].size();
    g[u].push_back(Edge(u,v,c,vr));
    g[v].push_back(Edge(v,u,0,ur));
}

void buildRow(int r,int n,int m,int all){
    int mid = r+all;
    for(int i = 1; i <= m; i++){
        if(s[r][i]!='.'){
            int u = (r-1)*m+i;
            if(s[r][i]!='T') addEdge(n*m+u,mid,inf);
            if(s[r][i]!='S') addEdge(mid,u,inf);
        }
    }
}

void buildCow(int c,int n,int m,int all){
    int mid = all+c;
    for(int i = 1; i <= n; i++){
        if(s[i][c]!='.'){
            int u = (i-1)*m+c;
            if(s[i][c]!='T') addEdge(n*m+u,mid,inf);
            if(s[i][c]!='S') addEdge(mid,u,inf);
        }
    }
}

int bfs(int s,int e,int n){
    for(int i = 1; i <= n; i++) depth[i] = 0;
    int h = 1, t = 0;
    depth[s] = 1; queue[++t] = s;
    while(h<=t){
        int u = queue[h++];
        for(Edge edge: g[u]){
            if(edge.c!=0 && depth[edge.v]==0){
                depth[edge.v] = depth[u]+1;
                queue[++t] = edge.v;
            }
        }
    }
    return depth[e]!=0;
}

int min(int a,int b){ return a>b?b:a; }

int dfs(int s,int e,int upper){
    if(s==e || upper==0) return upper;
    else{
        int sumFlow = 0;
        for(Edge& edge: g[s]){
            if(upper==0) break;
            if(edge.c!=0 && depth[edge.v]==depth[s]+1){
                int v = edge.v;
                int add = dfs(v,e,min(edge.c,upper));
                if(add!=0) depth[v] = 0;
                Edge& revE = g[edge.v][edge.rev];
                sumFlow += add, revE.c += add;
                upper -= add, edge.c -= add;
            }
        }
        return sumFlow;
    }
}

int dinic(int s,int e,int n){
    int res = 0;
    while(bfs(s,e,n)!=0){
        int add = -1;
        while(add!=0){
            add = dfs(s,e,inf);
            res += add;
        }
    }
    return res;
}

int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; i++) scanf("%s",s[i]+1);
    //in point: id, out point: id+n*m
    int all = 2*n*m;
    for(int i = 1; i <= n; i++) buildRow(i,n,m,all);
    all += n;
    for(int i = 1; i <= m; i++) buildCow(i,n,m,all);
    all += m;
    int x0 = -1, y0 = -1;
    int x1 = -1, y1 = -1;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(s[i][j]=='S'){
                x0 = i, y0 = j;
            }
            else if(s[i][j]=='T'){
                x1 = i, y1 = j;
            }
            else{}
            if(s[i][j]!='.'){
                int u = (i-1)*m+j;
                int inf = 1e8+7;
                if(s[i][j]!='o') addEdge(u,n*m+u,inf);
                else addEdge(u,n*m+u,1);
            }
        }
    }
    if(x0==x1 || y0==y1){
        printf("-1\n");
        return 0;
    }
    int s = (x0-1)*m+y0, e = ((x1-1)*m+y1)+n*m;
    printf("%d\n",dinic(s,e,all));
    return 0;
}


Measured memory_bytes: 6428