#include <bits/stdc++.h>
#define rep(n) for(int I = 0; (I) < (n); ++(I) )
#define repeat(i, n) for( int i = 0; (i) < (n); ++(i) )
#define repeat_to(i, n) for( int i = 0; (i) <= (n); ++(i) )
#define repeat_from(i, m, n) for( int i = (m); (i) < (n); ++(i) )
#define repeat_from_to(i, m, n) for( int i = (m); (i) <= (n); ++(i) )
#define el cout << endl
#define dump(x) cout << " " << #x << "=" << x
#define vdump(v) for(size_t T=0; T<v.size(); ++T){cout << " " << #v << "[" << T << "]=" << v[T];} cout << endl
using namespace std;
using lint = long long;
using ld = long double;

/****************** グラフ ******************/
// 諸宣言
using Weight = lint;
using Flow = lint;
struct Edge {
    int src, dst;
    Weight weight;
    Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
    Edge(int s, int d, Weight w, Flow c) : src(s), dst(d), weight(w), cap(c) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;

void add_edge(Graph &g, int a, int b, Weight w = 1, Flow c = 1) {
    g[a].emplace_back(a, b, w, c);
    g[b].emplace_back(b, a, w, c);
}
void add_arc(Graph &g, int a, int b, Weight w = 1, Flow c = 1) {
    g[a].emplace_back(a, b, w, c);
}


struct dinic {
    int n, s, t;
    vector<int> level, prog, que;
    vector<vector<Flow>> cap, flow;
    vector<vector<int>> g;
    Flow inf;
    dinic(const Graph &graph)
        : n(graph.size()),
          cap(n, vector<Flow>(n)),
          flow(n, vector<Flow>(n)),
          g(n, vector<int>()),
          inf(numeric_limits<Flow>::max() / 8) {
        for (int i = 0; i < n; i++) {
            for (auto &e : graph[i]) {
                int u = e.src, v = e.dst;
                Flow c = e.cap;
                cap[u][v] += c;
                cap[v][u] += c;
                flow[v][u] += c;
                g[u].push_back(v);
                g[v].push_back(u);
            }
        }
    }
    inline Flow residue(int u, int v) { return cap[u][v] - flow[u][v]; }
    Flow solve(int s_, int t_) {
        this->t = t_, this->s = s_;
        que.resize(n + 1);
        Flow res = 0;
        while (levelize()) {
            prog.assign(n, 0);
            res += augment(s, inf);
        }
        return res;
    }
    bool levelize() {
        int l = 0, r = 0;
        level.assign(n, -1);
        level[s] = 0;
        que[r++] = s;
        while (l != r) {
            int v = que[l++];
            if (v == t) break;
            for (const int &d : g[v])
                if (level[d] == -1 && residue(v, d) != 0) {
                    level[d] = level[v] + 1;
                    que[r++] = d;
                }
        }
        return level[t] != -1;
    }
    Flow augment(int v, Flow lim) {
        Flow res = 0;
        if (v == t) return lim;
        for (int &i = prog[v]; i < (int)g[v].size(); i++) {
            const int &d = g[v][i];
            if (residue(v, d) == 0 || level[v] >= level[d]) continue;
            const Flow aug = augment(d, min(lim, residue(v, d)));
            flow[v][d] += aug;
            flow[d][v] -= aug;
            res += aug;
            lim -= aug;
            if (lim == 0) break;
        }
        return res;
    }
};

int main(void) {
    int h, w; cin >> h >> w;
    vector<string> a(h);
    Graph g(h+w+2);
    
    const int START = 0;
    const int GOAL  = h+w+1;
    const lint INF   = numeric_limits<Flow>::max() / 4;
    auto node_h = [&h, &w] (int x) {return x+1;};
    auto node_w = [&h, &w] (int y) {return h+y+1;};
    
    repeat(i, h) {
        cin >> a[i];
        repeat(j, w) {
            if (a[i][j] != '.') {
                add_edge(g, node_h(i), node_w(j), 1, 1);
            }
            if (a[i][j] == 'S') {
                add_edge(g, START, node_h(i), 1, INF);
                add_edge(g, START, node_w(j), 1, INF);
            }
            else if (a[i][j] == 'T') {
                add_edge(g, GOAL, node_h(i), 1, INF);
                add_edge(g, GOAL, node_w(j), 1, INF);
            }
        }
    }
    
    
    dinic d(g);
    lint ans = d.solve(START, GOAL);
    cout << (ans >= INF ? -1 : ans) << endl;
    return 0;
}


Measured memory_bytes: 1792