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();
    }
    auto rcCap = new long[][](h, w);
    auto crCap = new long[][](w, h);
    long[] s, t;
    foreach(i; iota(h)) {
        foreach(j; iota(w)) {
            if(a[i][j] == 'o') {
                rcCap[i][j] = 1;
                crCap[j][i] = 1;
            }
            if(a[i][j] == 'S') {
                s = [i, j];
                rcCap[i][j] = long.max/4;
                crCap[j][i] = long.max/4;
            }
            if(a[i][j] == 'T') {
                t = [i, j];
            }
        }
    }
    if(s[0] == t[0] || s[1] == t[1]) {
        writeln(-1);
        return;
    }

    long dfs(long i, long cap, ref bool[] rUsed, ref bool[] cUsed, bool Row) {
        if(Row) {
            if(i == t[0]) {
                return cap;
            }
            if(rUsed[i]) {
                return 0;
            }
            rUsed[i] = true;
            foreach(col, c; rcCap[i]) {
                if(c == 0) {
                    continue;
                }
                auto ret = dfs(col, min(c, cap), rUsed, cUsed, false);
                if(ret > 0) {
                    rcCap[i][col] -= ret;
                    return ret;
                }
            }
        } else {
            if(i == t[1]) {
                return cap;
            }
            if(cUsed[i]) {
                return 0;
            }
            cUsed[i] = true;
            foreach(row, c; crCap[i]) {
                if(c == 0) {
                    continue;
                }
                auto ret = dfs(row, min(c, cap), rUsed, cUsed, true);
                if(ret > 0) {
                    crCap[i][row] -= ret;
                    return ret;
                }
            }
        }
        return 0;
    }

    long ans;
    while(true) {
        auto rUsed = new bool[](h);
        auto cUsed = new bool[](w);
        auto ret = dfs(s[0], long.max, rUsed, cUsed, true);
        if(ret == 0) {
            break;
        }
        ans += ret;
    }
    writeln(ans);
}

