// {{{
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define ALL(x) begin(x),end(x)
#define SZ(x) ((int)x.size())
#define FOR(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
using LL=long long;
using PII=pair<int,int>;
using VI=vector<int>;

#ifdef FEI
template<typename T>
void _dump(const char* s, T&& head) { cerr<<s<<"="<<head<<endl; }
template<typename T, typename... Args>
void _dump(const char* s, T&& head, Args&&... tail) {
    int c=0;
    while (*s!=',' || c!=0) {
        if (*s=='(' || *s=='[' || *s=='{') c++;
        if (*s==')' || *s==']' || *s=='}') c--;
        cerr<<*s++;
    }
    cerr<<"="<<head<<", ";
    _dump(s+1, tail...);
}

#define dump(...) do { \
    fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \
    _dump(#__VA_ARGS__, ##__VA_ARGS__); \
} while(0);

template<typename Iter>
ostream& _out(ostream &s, Iter b, Iter e) {
    s<<'[';
    for (auto it=b; it!=e; it++) s<<(it==b?"":",")<<*it;
    s<<']';
    return s;
}

template<typename A, typename B>
ostream& operator <<(ostream &s, const pair<A,B> &p) { return s<<"("<<p.first<<","<<p.second<<")";}
template<typename T>
ostream& operator <<(ostream &s, const vector<T> &x) { return _out(s,ALL(x)); }
template<typename T, size_t N>
ostream& operator <<(ostream &s, const array<T,N> &x) { return _out(s,ALL(x)); }
template<typename T>
ostream& operator <<(ostream &s, const set<T> &x) { return _out(s,ALL(x)); }
template<typename A, typename B>
ostream& operator <<(ostream &s, const map<A,B> &x) { return _out(s,ALL(x)); }
#else
#define dump(...)
#endif

template<typename T>
void _R(T &x) { cin>>x; }
void _R(int &x) { scanf("%d",&x); }
void _R(LL &x) { scanf("%" PRId64,&x); }
void _R(double &x) { scanf("%lf",&x); }
void _R(char &x) { scanf(" %c",&x); }
void _R(char *x) { scanf("%s",x); }

void R(){}
template<typename T, typename... X>
void R(T& head, X&... tail) { _R(head); R(tail...); }
// }}}

const int N = 105;
char g[N][N];
int n, m;
vector<int> to[N*N*2];
unordered_map<int, int> flow[N*N*2];
int from[N*N*2];

int id(int i, int j) { return i*m+j; }


int main() {
    R(n, m);
    for (int i=0; i<n; i++) R(g[i]);

    int sx=-1, sy=-1, tx=-1, ty=-1, sid, tid;
    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++)
            if (g[i][j] == 'S') {
                sx = i, sy = j;
                g[i][j] = 'o';
            }
            else if (g[i][j] == 'T') {
                tx = i, ty = j;
                g[i][j] = 'o';
            }

    if (sx == tx || sy == ty) {
        cout << -1 << endl;
        return 0;
    }

    sid = id(sx, sy); tid = id(tx, ty);
    int tot = n*m;

    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++)
            if (g[i][j] == 'o') {
                for (int k=i+1; k<n; k++)
                    if (g[k][j] == 'o') {
                        int f = id(i, j), t = id(k, j);
                        to[f+tot].push_back(t);
                        to[t+tot].push_back(f);
                        flow[f+tot][t] = 1;
                        flow[t+tot][f] = 1;
                    }
                for (int k=j+1; k<m; k++)
                    if (g[i][k] == 'o') {
                        int f = id(i, j), t = id(i, k);
                        to[f+tot].push_back(t);
                        to[t+tot].push_back(f);
                        flow[f+tot][t] = 1;
                        flow[t+tot][f] = 1;
                    }
            }

    for (int i=0; i<tot; i++) {
        to[i].push_back(i+tot);
        flow[i][i+tot] = 1;
    }

    for (int i=0; i<n; i++)
        for (int j=0; j<m; j++) {
            int now = id(i, j);
            dump(to[now], to[now+tot]);
        }




    flow[sid][sid+tot] = tot;

    int ans = 0;

    while (1) {
        queue<int> q;
        memset(from, -1, sizeof(from));
        q.push(sid);
        from[sid] = sid;

        while (q.size() && from[tid] == -1) {

            int now = q.front();
            q.pop();
            dump(now);

            for (int next : to[now]) {
                if (from[next] == -1 && flow[now][next] > 0) {
                    from[next] = now;
                    q.push(next);
                }
            }
        }

        if (from[tid] == -1) break;

        ans++;
        dump("__________");

        for (int now = tid; now != sid; now = from[now]) {
            dump(now/m, now%m, from[now]/m, from[now]%m);
            flow[ from[now] ][now]--;
            flow[now][ from[now] ]++;
        }
    }

    cout << ans << endl;




    return 0;
}


Measured memory_bytes: 92568