#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
constexpr lint INF = 100000000;

template<typename T>
class Maxflow
{
    struct edge { int to; T cap; int rev; };
    std::vector<std::vector<edge> > edges;
    std::vector<int> level;
    std::vector<int> iter;
    std::vector<int> used;

    void bfs(int s)
    {
        level = std::vector<int>(edges.size(), -1);
        std::queue<int> q;
        level[s] = 0;
        q.push(s);
        while (!q.empty())
        {
            int v = q.front();
            q.pop();
            for (edge &e : edges[v])
            {
                if (e.cap > 0 && level[e.to] < 0)
                {
                    level[e.to] = level[v] + 1;
                    q.push(e.to);
                }
            }
        }
    }

    T dfs_d(int v, int goal, T f)
    {
        if (v == goal) return f;
        for (int &i = iter[v]; i < (int)edges[v].size(); i++)
        {
            edge &e = edges[v][i];
            if (e.cap > 0 && level[v] < level[e.to])
            {
                T d = dfs_d(e.to, goal, std::min(f, e.cap));
                if (d > 0)
                {
                    e.cap -= d;
                    edges[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

    T dfs_ff(int v, int goal, T f)
    {
        if (v == goal) return f;
        used[v] = true;
        for (int i = 0; i < (int)edges[v].size(); i++)
        {
            edge &e = edges[v][i];
            if (e.cap > 0 && !used[e.to])
            {
                T d = dfs_ff(e.to, goal, std::min(f, e.cap));
                if (d > 0)
                {
                    e.cap -= d;
                    edges[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

public:
    Maxflow(int N) { edges.resize(N); }
    void add_edge(int from, int to, T capacity)
    {
        edges[from].push_back(edge{to, capacity, (int)edges[to].size()});
        edges[to].push_back(edge{from, (T)0, (int)edges[from].size() - 1});
    }

    T Dinic(int s, int t)
    {
        constexpr T INF = std::numeric_limits<T>::max();
        T flow = 0;
        while (true)
        {
            bfs(s);
            if (level[t] < 0) return flow;
            iter = std::vector<int>(edges.size(), 0);
            T f;
            while ((f = dfs_d(s, t, INF)) > 0) flow += f;
        }
    }

    T FF(int s, int t)
    {
        constexpr T INF = std::numeric_limits<T>::max();
        T flow = 0;
        while (true)
        {
            used = std::vector<int>(edges.size(), 0);
            T f = dfs_ff(s, t, INF);
            if (f == 0) return flow;
            flow += f;
        }
    }
};

int H, W;

int main()
{
    cin >> H >> W;
    int N = H*W;
    vector<vector<char> > a(H, vector<char>(W));
    for (auto &vec : a) for (auto &v : vec) cin >> v;

    Maxflow<lint> mf(N*2);

    int start, end;
    for (int i=0; i<H; i++)
    {
        vector<int> ok;
        for (int j=0; j<W; j++)
        {
            if (a[i][j] == 'S' || a[i][j] == 'T')
            {
                mf.add_edge(i*W+j, i*W+j+N, INF);
                mf.add_edge(i*W+j+N, i*W+j, INF);
                if (a[i][j] == 'S')
                {
                    start = i*W+j;
                }
                else
                {
                    end = i*W+j;
                }
            }
            if (a[i][j] == 'o')
            {
                mf.add_edge(i*W+j, i*W+j+N, 1);
                mf.add_edge(i*W+j+N, i*W+j, 1);
            }

            if (a[i][j] != '.') ok.push_back(j);
        }
        for (int x=0; x<ok.size(); x++)
        {
            for (int y=0; y<x; y++)
            {
                mf.add_edge(i*W+ok[x], i*W+ok[y], INF);
                mf.add_edge(i*W+ok[y], i*W+ok[x], INF);
            }
        }
    }
    for (int j=0; j<W; j++)
    {
        vector<int> ok;
        for (int i=0; i<H; i++)
        {
            if (a[i][j] != '.') ok.push_back(i);
        }
        for (int x=0; x<ok.size(); x++)
        {
            for (int y=0; y<x; y++)
            {
                mf.add_edge(ok[x]*W+j+N, ok[y]*W+j+N, INF);
                mf.add_edge(ok[y]*W+j+N, ok[x]*W+j+N, INF);
            }
        }
    }
    lint cost = mf.Dinic(start, end);
    if (cost < INF) cout << cost << endl;
    else cout << -1 << endl;
}

Measured memory_bytes: 122112