#pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL

#include <prettyprint.hpp>

#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template<typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template<typename T>
void print(T x) {
    cout << x << endl;
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9 + 7;

// Dinic
template<typename T>
struct Dinic {

    struct Graph {
        int to, rev;
        T cap;

        Graph(int t, int r, const T &c) : to(t), rev(r), cap(c) {};
    };

    vector<vector<Graph>> g;
    vector<int> level;
    vector<int> iter;
    int sz;

    Dinic(int N) : sz(N) {
        g.resize(N);
        iter.resize(N);
        level.resize(N);
    }

    void add_edge(int from, int to, T cap) {
        g[from].eb(to, (int) g[to].size(), cap);
        g[to].eb(from, (int) g[from].size() - 1, 0);
    }

    T operator()(int start, int end) {
        T flow = 0;
        while(true) {
            bfs(start);
            if (level[end] < 0) break;

            iter.assign(sz, 0);

            T f;
            while(f = dfs(start, end, inf) > 0) {
                flow += f;
            }
        }
        return flow;
    }

private:
    void bfs(int s) {
        level.assign(sz, -1);

        level[s] = 0;
        queue<int> q;
        q.push(s);

        while(!q.empty()) {
            int v = q.front(); q.pop();
            for(auto &e: g[v]) {
                int t = e.to;
                if (e.cap > 0 && level[t] < 0) {
                    level[t] = level[v] + 1;
                    q.push(t);
                }
            }

        }
    }

    T dfs(int v, int t, T flow) {
        if (v == t || flow == 0) return flow;

        for (int &i = iter[v]; i < g[v].size(); ++i) {
            auto &e = g[v][i];
            if(e.cap <= 0 || level[v] >= level[e.to]) continue;
            if (T f = dfs(e.to, t, min(flow, e.cap)) > 0) {
                e.cap -= f;
                g[e.to][e.rev].cap += f;
                return f;
            }
        }

        return 0;
    }
};

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int H, W;
    cin >> H >> W;

    char grid[H][W];
    REP(i, H) REP(j, W) {
            cin >> grid[i][j];
    }

    Dinic<int> dnc(H + W + 2);

    int s = 0, t = H + W + 1;
    Pin sp, tp;
    REP(h, H) REP(w, W) {
            if (grid[h][w] == '.') continue;

            if (grid[h][w] == 'S') {
                dnc.add_edge(s, 1 + w, inf);
                dnc.add_edge(s, 1 + h + W, inf);
                sp = {h, w};
            } else if (grid[h][w] == 'T') {
                dnc.add_edge(1 + w, t, inf);
                dnc.add_edge(1 + h + W, t, inf);
                tp = {h, w};
            } else {
                dnc.add_edge(1 + w, 1 + W + h, 1);
                dnc.add_edge(1 + W + h, 1 + w, 1);
            }

        }

    if (sp.fi == tp.fi || sp.se == tp.se) {
        print(-1);
        return 0;
    }

    print(dnc(s, t));
}


Measured memory_bytes: 896