#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <chrono>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

const ll SIZE = 1e3 + 100, INF = 1e9 + 10, MOD = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll n, a, b, c, d, dp[SIZE][2];
vector<ll> fact;
vector<vector<ll>> gr;
pair<ll, ll> st, nd;
char grid[101][101];
struct edge {
    ll bal, u, v;
};

vector<edge> edges;


void input() {
    ll n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> grid[i][j];
            ll curV = i * m + j;
            if (grid[i][j] == 'S') st = { i, j };

            if (grid[i][j] == 'T') nd = { i, j };
        }
    }

    ll add = (n * m + 2);
    gr.resize(n + m + 10);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (grid[i][j] == 'o') {
                edges.push_back({ 1, i + 2, j + n + 2 });
                edges.push_back({ 1, j + n + 2, i + 2 });
            }
            if (grid[i][j] == 'S') {
                edges.push_back({ INF, 0, i + 2});
                edges.push_back({ INF, i + 2, 0 });

                edges.push_back({ INF, 0, j + n + 2 });
                edges.push_back({ INF, j + n + 2, 0 });
            }
            if (grid[i][j] == 'T') {
                edges.push_back({ INF, 1, i + 2 });
                edges.push_back({ INF, i + 2, 1 });

                edges.push_back({ INF, 1, j + n + 2 });
                edges.push_back({ INF, j + n + 2, 1 });
            }
        }
    }

    for (int i = 0; i < edges.size(); i += 2) {
        ll curV = edges[i].v, curU = edges[i].u;
        gr[curV].push_back(i);
        gr[curU].push_back(i + 1);
    }
}

ll pw(ll a, ll b, ll md) { ll res = 1; while (b) { if (b & 1) { res = (a*res) % md; }a = (a*a) % md; b >>= 1; }return(res); }

ll bfs() {
    queue<pair<ll, ll>> q;
    q.push({ 0, INF });
    vector<bool> used(gr.size());
    vector<ll> par(gr.size());
    ll val = 0;
    used[0] = 1;
    while (!q.empty()) {
        pair<ll, ll> v = q.front();
        q.pop();

        for (auto to : gr[v.first]) {
            ll u = edges[to].u, bal = edges[to].bal;
            if (!used[u] && bal != 0) {
                used[u] = 1;
                ll vl = min(v.second, bal);
                q.push({ u, vl });
                par[u] = to;
                if (u == 1) val = vl;
            }
        }
    }

    if (used[1]) {
        ll v = 1;
        while (v != 0) {
            ll edg = par[v];
            edges[edg].bal -= val;
            edges[edg ^ 1].bal += val;
            v = edges[edg].v;
        }

        return val;
    }
    else {
        return 0;
    }
}

void sol() {
    ll pr = 0;
    ll q = bfs();
    while (q) {
        pr += q;
        q = bfs();
    }

    if (pr >= INF) {
        cout << "-1";
    }
    else {
        cout << pr;
    }
}

int main()
{
    fastInp;

    ll t = 1;

    while (t--) {
        input();
        sol();
    }

    return 0;
}

Measured memory_bytes: 4256