#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 ford_fulkerson {
    using Matrix = vector<vector<Flow>>;
    int n, t;
    vector<int> vis;
    vector<vector<Flow>> cap, flow;
    vector<vector<int>> g;
    const Flow INF;
    ford_fulkerson(int n)
        : n(n),
          cap(n, vector<Flow>(n)),
          flow(n, vector<Flow>(n)),
          g(n, vector<int>()),
          INF(numeric_limits<Flow>::max() / 8) {}
    ford_fulkerson(const Graph &graph) : ford_fulkerson(graph.size()) {
        for (int i = 0; i < n; ++i)
            for (auto &e : graph[i]) add_edge(e.src, e.dst, e.cap);
    }
    void add_edge(int u, int v, Flow c) {
        cap[u][v] += c;
        cap[v][u] += c;
        flow[v][u] += c;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    Flow solve(int s, int t) {
        this->t = t;
        Flow res = 0, aug = 1;
        while (aug > 0) {
            vis.assign(n, false);
            res += (aug = augment(s, INF));
        }
        return res;
    }
    Flow augment(const int &v, const Flow &lim) {
        vis[v] = true;
        if (v == t) return lim;
        for (const int &d : g[v]) {
            if (vis[d] || flow[v][d] == cap[v][d]) continue;
            Flow aug = augment(d, min(lim, cap[v][d] - flow[v][d]));
            flow[v][d] += aug;
            flow[d][v] -= aug;
            if (aug > 0) return aug;
        }
        return 0;
    }
};

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);
            }
        }
    }
    
    
    ford_fulkerson f(g);
    lint ans = f.solve(START, GOAL);
    cout << (ans >= INF ? -1 : ans) << endl;
    return 0;
}


Measured memory_bytes: 1792