#include <bits/stdc++.h>

#ifndef LOCAL_
#define fprintf if( false ) fprintf
#endif // LOCAL_
// #define dump() fprintf(stderr, "#%s.%d\n", __func__, __LINE__);
#define dumpl(x1) fprintf(stderr, "#%s.%d (%s) = (%ld)\n", __func__, __LINE__, #x1, x1);
#define dumpll(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%ld, %ld)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define dumplll(x1, x2, x3) fprintf(stderr, "#%s.%d (%s, %s, %s) = (%ld, %ld, %ld)\n", __func__, __LINE__, #x1, #x2, #x3, x1, x2, x3);
#define dumpd(x1) fprintf(stderr, "#%s.%d (%s) = (%lf)\n", __func__, __LINE__, #x1, x1);
#define dumpdd(x1, x2) fprintf(stderr, "#%s.%d (%s, %s) = (%lf, %lf)\n", __func__, __LINE__, #x1, #x2, x1, x2);
#define loop for(;;)

template<typename T> void scan1(T& x) { fprintf(stderr, "unknown type\n"); }
template<> void scan1(long& x) { if( scanf("%ld", &x) < 0 ) exit(0); }
void scan() {}
template<typename Head, typename... Tail>
void scan(Head& x, Tail&... xs) {
  scan1(x); scan(xs...);
}

struct Edge {
   Edge(long s_, long t_, long cap_, long rev_)
      : s(s_), t(t_), cap(cap_), rev(rev_) {}
   long s, t, cap, rev;
};
std::vector<Edge> graph[40000];
void addEdge(long s, long t, long cap) {
   long x = graph[s].size();
   long y = graph[t].size();
   graph[s].push_back(Edge(s, t, cap, y));
   graph[t].push_back(Edge(t, s, 0, x));
}
bool used[40000];
void init() {
   for(long i = 0; i < 40000; ++i) {
      used[i] = false;
   }
}
long dfs(long s, long t, long f) {
   if( s == t ) return f;
   used[s] = true;
   for(Edge& e : graph[s]) {
      if( used[e.t] ) continue;
      if( e.cap == 0 ) continue;
      long x = dfs(e.t, t, std::min(f, e.cap));
      if( x == 0 ) continue;
      e.cap -= x;
      graph[e.t][e.rev].cap += x;
      return x;
   }
   return 0;
}
long maxFlow(long s, long t) {
   long res = 0;
   loop {
      init();
      long f = dfs(s, t, (1LL << 60));
      res += f;
      if( f == 0 ) break;
   }
   return res;
}

struct Solver {
   Solver() { fprintf(stderr, "--------Solver begin--------\n"); }
   ~Solver() { fprintf(stderr, "--------Solver end--------\n"); }
   void solve() {
      long h, w;
      scan(h, w);
      std::string field[128];
      for(long i = 0; i < h; ++i) {
         std::cin >> field[i];
      }
      long countX[128] = {};
      long countY[128] = {};
      long inf = (1LL << 60);
      long sr, sc, gr, gc;
      for(long r = 0; r < h; ++r) {
         for(long c = 0; c < w; ++c) {
            if( field[r][c] == '.' ) continue;
            if( field[r][c] == 'S' ) {
               sr = r; sc = c;
            }
            if( field[r][c] == 'T' ) {
               gr = r; gc = c;
            }
            long s = r * 100 + c;
            long t = 10000 + r * 100 + c;
            addEdge(s, t, 1);
            addEdge(t, 20000 + r, inf);
            addEdge(20000 + r, s, inf);
            addEdge(t, 21000 + c, inf);
            addEdge(21000 + c, s, inf);
            countY[r] += 1;
            countX[c] += 1;
         }
      }
      long res = maxFlow(10000 + sr * 100 + sc, gr * 100 + gc);
      if( res >= inf / 2 ) {
         puts("-1");
         return;
      }
      printf("%ld\n", res);
   }
};

int main() {
  loop std::unique_ptr<Solver>(new Solver())->solve();
}



Measured memory_bytes: 5760