import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

immutable inf = 10^^9 + 7;

alias Edge = Tuple!(int, "to", int, "cost", int, "rev");

void main() {
    int h, w;
    scan(h, w);

    auto adj = new Edge[][](h + w + 2, 0);

    int si, sj, ti, tj;

    void addEdge(int u, int v, int cap) {
        adj[u] ~= Edge(v, cap, adj[v].length.to!int);
        adj[v] ~= Edge(u, 0, adj[u].length.to!int - 1);
    }

    foreach (i ; 0 .. h) {
        auto line = readln.chomp;

        foreach (j ; 0 .. w) {
            int y = i + 1, x = h + j + 1;
            if (line[j] == 'S') {
                si = i, sj = j;
                addEdge(0, y, inf);
                addEdge(0, x, inf);
            }
            else if (line[j] == 'T') {
                ti = i, tj = j;
                addEdge(y, h + w + 1, inf);
                addEdge(x, h + w + 1, inf);
            }
            else if (line[j] == 'o') {
                addEdge(y, x, 1);
                addEdge(x, y, 1);
            }
        }
    }

    if (si == ti || sj == tj) {
        writeln(-1);
        return;
    }
    else {
        int ans = FordFulkerson(adj, 0, h + w + 1);
        writeln(ans);
    }
}

int FordFulkerson(Edge[][] adj, int s, int t) {
    int n = adj.length.to!int;
    bool[] visited = new bool[](n);

    int dfs (int u, int f) {
        if (u == t) {
            debug {
                writef("%d ", t);
            }
            return f;
        }

        visited[u] = 1;

        foreach (ref e ; adj[u]) {
            if (e.cost > 0 && !visited[e.to]) {
                int d = dfs(e.to, min(f, e.cost));

                if (d > 0) {
                    e.cost -= d;
                    adj[e.to][e.rev].cost += d;

                    debug {
                        writef("%d ", u);
                    }

                    return d;
                }
            }
        }

        return 0;
    }

    int F;

    while (true) {
        visited[] = 0;

        int f = dfs(s, inf);

        debug {
            writeln("flow: ", f);
        }

        if (f == 0) break;

        F += f;
    }

    return F;
}

void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}