import std.algorithm;
import std.array;
import std.ascii;
import std.bigint;
import std.complex;
import std.container;
import std.conv;
import std.functional;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
	return readInts()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}

void readlnTo(T...)(ref T t) {
    auto s = readln().split();
    assert(s.length == t.length);
    foreach(ref ti; t) {
        ti = s[0].to!(typeof(ti));
        s = s[1..$];
    }
}

class UnionFindTree {
public:
    this(size_t n) {
        parent = iota(n).array();
        rank = new size_t[](n);
    }
    size_t find(size_t n) {
        if(n == parent[n]) {
            return n;
        } else {
            parent[n] = find(parent[n]);
            return parent[n];
        }
    }
    void unite(size_t n, size_t m) {
        auto p1 = find(n);
        auto p2 = find(m);
        if(n == m) {
            return;
        }
        if(rank[p1] < rank[p2]) {
            swap(p1, p2);
        }
        if(rank[p1] == rank[p2]) {
            ++rank[p1];
        }
        parent[p2] = p1;
    }
private:
    size_t[] parent;
    size_t[] rank;
}
unittest {
    auto uft = new UnionFindTree(10);
    foreach(i; iota(10)) {
        assert(uft.find(i) == i);
    }
    foreach(i; iota(10).filter!(a => a%3 == 0).drop(1)) {
        uft.unite(i, i-3);
    }
    foreach(i; iota(10).filter!(a => a%3 == 0)) {
        assert(uft.find(0) == uft.find(i));
    }
    foreach(i; iota(10).filter!(a => a%3 != 0)) {
        assert(uft.find(0) != uft.find(i));
    }
}

const real eps = 1e-10;

const long p = 1_000_000_000 + 7;

void main(){
    long h, w;
    readlnTo(h, w);
    string[] a;
    foreach(i; iota(h)) {
        a ~= readln.chomp();
    }
    alias Node = long[];
    long[Node][Node] g;
    long[] s, t;
    foreach(i; iota(h)) {
        foreach(j; iota(w)) {
            if(a[i][j] == '.') {
                continue;
            }
            if(a[i][j] == 'S') {
                s = [i, j];
            } else if(a[i][j] == 'T') {
                t = [i, j];
            } else {
                g[[i, j, 0].idup][[i, j, 1].idup] = 1;
                g[[i, j, 1].idup][[i, j, 0].idup] = 1;
            }
            foreach(k; iota(h)) {
                if(k == i) {
                    continue;
                }
                if(a[k][j] == '.') {
                    continue;
                }
                g[[i, j, 0].idup][[k, j, 0].idup] = long.max/4;
            }
            foreach(k; iota(w)) {
                if(k == j) {
                    continue;
                }
                if(a[i][k] == '.') {
                    continue;
                }
                g[[i, j, 1].idup][[i, k, 1].idup] = long.max/4;
            }
        }
    }
    if(s[0] == t[0] || s[1] == t[1]) {
        writeln(-1);
        return;
    }
    g[s.idup][(s ~ 0).idup] = long.max/4;
    g[s.idup][(s ~ 1).idup] = long.max/4;
    g[(s ~ 0).idup][s.idup] = 0;
    g[(s ~ 1).idup][s.idup] = 0;
    g[(t ~ 0).idup][t.idup] = long.max/4;
    g[(t ~ 1).idup][t.idup] = long.max/4;
    g[t.idup][(t ~ 0).idup] = 0;
    g[t.idup][(t ~ 1).idup] = 0;

    long dfs(const Node n, const Node m, ref bool[Node] used, long maxFlow) {
        if(n == m) {
            return maxFlow;
        }

        if(n in used) {
            return 0;
        }
        used[n] = true;
        foreach(l, ref c; g[n]) {
            if(c == 0) {
                continue;
            }
            long flow = dfs(l, m, used, min(maxFlow, c));
            if(flow > 0) {
                c -= flow;
                g[l][n] += flow;
                return flow;
            }
        }
        return 0;
    }

    long ans;
    while(true) {
        bool[Node] used;
        long flow = dfs(s, t, used, long.max/4);
        if(flow == 0) {
            break;
        }
        ans += flow;
    }
    writeln(ans);
}

