#include <bits/stdc++.h>
using namespace std;

vector <int> adia[210];
int viz[210];
int S1, S2, D1, D2;

bool dfs(int nod)
{
    if (nod == D1 || nod == D2)
        return true;
    viz[nod] = 1;
    for (int i(0); i < adia[nod].size(); i++) {
        if (!viz[adia[nod][i]] && dfs(adia[nod][i])) {
            adia[adia[nod][i]].push_back(nod);
            swap(adia[nod][i], adia[nod].back());
            adia[nod].pop_back();
            return true;
        }
    }
    return false;
}

int main()
{
    int n, m;
    cin >> n >> m;

    for (int i(1); i <= n; i++) {
        for (int j(1); j <= m; j++) {
            char c;
            cin >> c;
            if (c == 'S')
                S1 = i, S2 = 100 + j;
            else if (c == 'T')
                D1 = i, D2 = 100 + j;
            else if (c == 'o') {
                adia[i].push_back(100 + j);
                adia[100 + j].push_back(i);
            }
        }
    }

    int ans(0);

    if (S1 == D1 || S2 == D2) {
        cout << -1;
        return 0;
    }

    int ok(1);

    while (ok) {
        ok = 0;
        if (dfs(S1))
            ok = 1, ans++;
        memset(viz, 0, sizeof viz);
        if (dfs(S2))
            ok = 1, ans++;
        memset(viz, 0, sizeof viz);
    }

    cout << ans << '\n';
    return 0;
}
