#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
// const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1},
//           dx[] = {0, -1, -1, -1, 0, 1, 1, 1};

struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    cerr << fixed << setprecision(10);
  }
} iosetup;
/*-------------------------------------------------*/
template <typename T>
struct Dinic {
  struct Edge {
    int dst, rev;
    T cap;
    Edge(int dst, T cap, int rev) : dst(dst), cap(cap), rev(rev) {}
  };

  vector<vector<Edge> > graph;

  Dinic(int n) : graph(n), level(n), itr(n) {}

  void add_edge(int src, int dst, T cap) {
    graph[src].emplace_back(dst, cap, graph[dst].size());
    graph[dst].emplace_back(src, 0, graph[src].size() - 1);
  }

  T maximum_flow(int s, int t, T limit) {
    T res = 0;
    while (true) {
      bfs(s);
      if (level[t] == -1) return res;
      fill(ALL(itr), 0);
      T tmp;
      while ((tmp = dfs(s, t, limit)) > 0) res += tmp;
    }
  }

private:
  vector<int> level, itr;

  void bfs(int s) {
    fill(ALL(level), -1);
    queue<int> que;
    level[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
      int ver = que.front(); que.pop();
      for (Edge e : graph[ver]) if (level[e.dst] == -1 && e.cap > 0) {
        level[e.dst] = level[ver] + 1;
        que.emplace(e.dst);
      }
    }
  }

  T dfs(int ver, int t, T flow) {
    if (ver == t) return flow;
    for (; itr[ver] < graph[ver].size(); ++itr[ver]) {
      Edge &e = graph[ver][itr[ver]];
      if (level[ver] < level[e.dst] && e.cap > 0) {
        T tmp = dfs(e.dst, t, min(flow, e.cap));
        if (tmp > 0) {
          e.cap -= tmp;
          graph[e.dst][e.rev].cap += tmp;
          return tmp;
        }
      }
    }
    return 0;
  }
};

int main() {
  int h, w; cin >> h >> w;
  vector<string> a(h); REP(i, h) cin >> a[i];
  map<pair<int, int>, int> mp;
  REP(i, h) REP(j, w) {
    if (a[i][j] == 'o') {
      int sz = mp.size();
      mp[{i, j}] = sz;
    }
  }
  int sy, sx, ty, tx;
  REP(i, h) REP(j, w) {
    if (a[i][j] == 'S') {
      sy = i;
      sx = j;
    }
    if (a[i][j] == 'T') {
      ty = i;
      tx = j;
    }
  }
  if (sy == ty || sx == tx) {
    cout << -1 << '\n';
    return 0;
  }
  int n = mp.size(), S = n * 2, T = n * 2 + 1;
  mp[{ty, tx}] = T;
  Dinic<int> dinic(n * 2 + 2);
  REP(i, n) dinic.add_edge(i, n + i, 1);
  REP(i, h) REP(j, w) {
    if (a[i][j] == '.' || a[i][j] == 'T') continue;
    int src = (a[i][j] == 'S' ? S : n + mp[{i, j}]);
    for (int y = i - 1; y >= 0; --y) {
      if (a[y][j] == 'o' || a[y][j] == 'T') dinic.add_edge(src, mp[{y, j}], 1);
    }
    for (int x = j - 1; x >= 0; --x) {
      if (a[i][x] == 'o' || a[i][x] == 'T') dinic.add_edge(src, mp[{i, x}], 1);
    }
    FOR(y, i + 1, h) {
      if (a[y][j] == 'o' || a[y][j] == 'T') dinic.add_edge(src, mp[{y, j}], 1);
    }
    FOR(x, j + 1, w) {
      if (a[i][x] == 'o' || a[i][x] == 'T') dinic.add_edge(src, mp[{i, x}], 1);
    }
  }
  cout << dinic.maximum_flow(S, T, INF) << '\n';
  return 0;
}


Measured memory_bytes: 63232