// #define STOPIT
#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_a_genius = 0; ngtkana_is_a_genius < int(n); ngtkana_is_a_genius++)
#define rep(i, begin, end) for (int i = int(begin); (i) < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto& a, auto b){ if (a > b) {a = b; return true;} return false; };
auto cmx = [](auto& a, auto b){ if (a < b) {a = b; return true;} return false; };
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) { std::cerr << " " << head; debug_impl(tail...); }
#ifndef STOPIT
#define debug(...)\
  do {\
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
    debug_impl(__VA_ARGS__);\
    std::cerr << std::noboolalpha;\
  } while (false)
#else
#define debug(...) {}
#endif

template <typename Flow>
struct residue_edge {
  int to; Flow cap, original_cap; int rev;
  residue_edge(int to, Flow cap, int rev):
    to(to), cap(cap), original_cap(cap), rev(rev){}
};

template <typename Flow>
std::ostream& operator<< (std::ostream& os, const residue_edge<Flow>& e){
  return os
    << "residue_edge{"
    << "to:" << e.to << ","
    << "cap:" << e.cap << ","
    << "rev:" << e.rev
    << "}";
}

template <typename Flow>
struct flow_edge {
  int to; Flow flow;
  flow_edge(int to, Flow flow) : to(to), flow(flow){}
};

template <typename Flow>
std::ostream& operator<< (std::ostream& os, const flow_edge<Flow>& e) {
  return os
    << "flow_edge{"
    << "to:" << e.to << ","
    << "flow:" << e.flow
    << "}";
}

template <typename Flow>
class dinic {
    int                                          n, source, sink;
    std::vector<bool>                            ckd;
    std::vector<int>                             level;
    std::vector<std::vector<residue_edge<Flow>>> graph;

    static constexpr Flow inf_flow = std::numeric_limits<Flow>::max();

    void bfs () {
      std::queue<int> que;
      que.emplace(source); level.at(source) = 0;
      while (!que.empty()) {
        auto crr = que.front(); que.pop();
        for (auto const& e : graph.at(crr)) {
          if (e.cap == 0) continue;
          int nxt = e.to;
          if (level.at(nxt) != -1) continue;
          que.push(nxt);
          level.at(nxt) = level.at(crr) + 1;
        }
      }
    }

    auto dfs (int crr, Flow f = inf_flow) {
      if (crr == sink) return f;
      ckd.at(crr) = true;
      for (auto& e : graph.at(crr)) {
        auto nxt = e.to;
        if (ckd.at(nxt) || e.cap == 0 || level.at(crr) >= level.at(nxt)) continue;
        auto d = dfs(nxt, std::min(f, e.cap));
        if (d > 0) {
          e.cap -= d;
          graph.at(nxt).at(e.rev).cap += d;
          return d;
        }
      }
      level.at(crr) = -1;
      return Flow(0);
    }

  public:
    dinic (int n, int source, int sink):
      n(n), source(source), sink(sink), graph(n){}

    void insert(int u, int v, Flow c) {
      // debug(u,v,c);
      graph.at(u).emplace_back(v, c, graph.at(v).size());
      graph.at(v).emplace_back(u, 0, graph.at(u).size() - 1);
    }

    auto build() {
      Flow ret = 0;
      while (true) {
        level.assign(n, -1);
        bfs();
        if (level.at(sink) == -1) return ret;
        ckd.assign(n, false);
        while (true) {
          auto f = dfs(source);
          if (f == 0) break;
          ret += f;
        }
      }
    }

    auto collect() const {
      std::vector<std::vector<flow_edge<Flow>>> ret(n);
      for (auto i = 0; i < n; i++) {
        for (auto const& e : graph.at(i)) {
          auto flow = e.original_cap - e.cap;
          if (flow > 0) ret.at(i).emplace_back(e.to, flow);
        }
      }
      return ret;
    }
};

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int h, w; std::cin >> h >> w;
  int n = h + w;
  int s = n, t = s + 1;
  int N = t+1;
  dinic<lint> dnc(N,s,t);

  int inf = std::numeric_limits<int>::max();
  rep(i,0,h) rep(j,0,w) {
    char c; std::cin >> c;
    if (c == 'S') {
      dnc.insert(s,i,inf);
      dnc.insert(s,h+j,inf);
    } else if (c=='T') {
      dnc.insert(i,t,inf);
      dnc.insert(h+j,t,inf);
    } else if (c=='o') {
      dnc.insert(i,h+j,1);
      dnc.insert(h+j,i,1);
    }
  }
  lint ans = dnc.build();
  if (inf <= ans) ans = -1;
  std::cout << ans << std::endl;
  return 0;
}

Measured memory_bytes: 1920