#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long

#define all(X) (X).begin(), (X).end()
#define reset(X) memset(X, 0, sizeof(X))
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define endl '\n'
#define fi first
#define se second

using namespace __gnu_pbds;
using namespace std;
const int mod = 1e9 + 7;
// const int mod=998244353;
//const int mod=1e9+9;
//const int INF=4e18+10;
const int INF = 4e18 + 10;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define read(n) \
int n;      \
cin >> n;
#define readv(x, n)   \
vector<int> x(n); \
for (auto &i : x) \
    cin >> i;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;

template <typename T1, typename T2>
void set_min(T1 &a, T2 b)
{
    a = min(a, (T1)b);
}

int multiply(int a, int b, int in_mod) { return (a * b) % in_mod; }
int mult_identity(int a) { return 1; }


auto power(auto a, int b, int in_mod)
{
    auto prod = mult_identity(a);
    auto mult = a % in_mod;
    while (b != 0)
    {
        if (b % 2)
        {
            prod = multiply(prod, mult, in_mod);
        }
        mult = multiply(mult, mult, in_mod);
        b /= 2;
    }
    return prod;
}
int mod_inv(int q, int in_mod)
{

    return power(q, in_mod - 2, in_mod);
}
const int MAXN = 300;
int adj[MAXN][MAXN];
int cost[MAXN][MAXN];
int capacity[MAXN][MAXN];
int n;

int bfs(int s, int t, vector<int>& parent) {
    fill(parent.begin(), parent.end(), -1);
    parent[s] = -2;
    queue<pair<int, int>> q;
    q.push({s, INF});

    while (!q.empty()) {
        int cur = q.front().first;
        int flow = q.front().second;
        q.pop();

        for (int j=0;j<n;j++) {
            int next = adj[cur][j];
            if(next)
            {
                next = j;
            if (parent[next] == -1 && capacity[cur][next]) {
                parent[next] = cur;
                int new_flow = min(flow, capacity[cur][next]);
                if (next == t)
                    return new_flow;
                q.push({next, new_flow});
            }
            }
        }
    }

    return 0;
}

int maxflow(int s, int t) {
    int flow = 0;
    vector<int> parent(n);
    int new_flow;

    while (new_flow = bfs(s, t, parent)) {
        flow += new_flow;
        int cur = t;
        while (cur != s) {
            int prev = parent[cur];
            capacity[prev][cur] -= new_flow;
            capacity[cur][prev] += new_flow;
            cur = prev;
        }
    }

    return flow;
}

void  solve()
{
    int h, w;
    cin>>h>>w;
    vector<vector<int>> a(h+1, vector<int>(w+1));

    n = h+w+3;
    pair<int, int> s, t;
    for(int i= 1;i<=h;i++)
    {
        for(int j= 1;j<=w;j++)
        {
            char ch;
            cin>>ch;
            if(ch == 'o')
            {
                adj[i][h+j] = 1;
                adj[h+j][i] = 1;
                cost[h+j][i] = 1;
                cost[i][h+j] = 1;
                capacity[h+j][i] = 1;
                capacity[i][h+j] = 1;
            }
            else if(ch == 'S')
            {
                adj[h+w + 1][i] = 1;
                adj[i][h+w+1] = 1;
                cost[h+w + 1][i] = INF;
                cost[i][h+w+1] = INF;
                capacity[h+w + 1][i] = INF;
                capacity[i][h+w+1] = INF;
                adj[h+w + 1][h+j] = 1;
                adj[h+j][h+w+1] = 1;
                cost[h+w + 1][h+j] = INF;
                cost[h+j][h+w+1] = INF;
                capacity[h+w + 1][h+j] = INF;
                capacity[h+j][h+w+1] = INF;
                s = {i,j};
            }
            else if(ch == 'T')
            {

                adj[h+w + 2][i] = 1;
                adj[i][h+w+2] = 1;
                cost[h+w + 2][i] = INF;
                cost[i][h+w+2] = INF;
                capacity[h+w + 2][i] = INF;
                capacity[i][h+w+2] = INF;
                adj[h+w + 2][h+j] = 1;
                adj[h+j][h+w+2] = 1;
                capacity[h+w + 2][h+j] = INF;
                capacity[h+j][h+w+2] = INF;
                t = {i,j};
            }


        }
    }



    //max flow :yay

    int ans = INF;

    int mxflow = maxflow(h+w+1, h+w+2);
    if(mxflow >=INF)
        cout<<-1<<endl;
    else
        cout<<mxflow<<endl;


}



signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cerr.tie(NULL);
#ifndef ONLINE_JUDGE
    if (fopen("INPUT.txt", "r"))
    {
        freopen("INPUT.txt", "r", stdin);
        freopen("OUTPUT.txt", "w", stdout);
    }
#endif
    auto clk = clock();
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    // -------------------------------------Code starts here---------------------------------------------------------------------

    int t = 1;
    // cin >> t;

    for (int test = 1; test <= t; test++)
    {
        // cout<<"Case #"<<test<<":\n";
        //cout<<"Trip #"<<test<<": ";
        // cout<<endl;
        solve();
        // solveB();

        // cout<<endl;
    }

    // -------------------------------------Code ends here------------------------------------------------------------------

    clk = clock() - clk;
    // cerr << fixed << setprecision(6) << "Time: " << ((long double)clk) / CLOCKS_PER_SEC << "\n";
    return 0;
}


Measured memory_bytes: 5152