#include<algorithm>
#include<cassert>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
typedef long long lint;
typedef vector<int>vi;
typedef pair<int,int>pii;
#define rep(i,n)for(int i=0;i<int(n);++i)

/**
 * Dinic's algorithm for maximum flow problem.
 * Header requirement: vector, queue
 * Verified by: ABC010-D(http://abc010.contest.atcoder.jp/submissions/602810)
 *              ARC031-D(http://arc031.contest.atcoder.jp/submissions/1050071)
 *              POJ 3155(http://poj.org/problem?id=3155)
 */
template<class T = int>
class Dinic {
private:
  struct edge {
    int to;
    T cap;
    int rev; // rev is the position of reverse edge in graph[to]
  };
  std::vector<std::vector<edge> > graph;
  std::vector<int> level;
  std::vector<int> iter;
  /* Perform bfs and calculate distance from s */
  void bfs(int s) {
    level.assign(level.size(), -1);
    std::queue<int> que;
    level[s] = 0;
    que.push(s);
    while (! que.empty()) {
      int v = que.front(); que.pop();
      for (int i = 0; i < graph[v].size(); ++i) {
	edge &e = graph[v][i];
	if (e.cap > 0 && level[e.to] == -1) {
	  level[e.to] = level[v] + 1;
	  que.push(e.to);
	}
      }
    }
  }
  /* search augment path by dfs.
     if f == -1, f is treated as infinity. */
  T dfs(int v, int t, T f) {
    if (v == t) {
      return f;
    }
    for (int &i = iter[v]; i < graph[v].size(); ++i) {
      edge &e = graph[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
	T newf = f == -1 ? e.cap : std::min(f, e.cap);
	T d = dfs(e.to, t, newf);
	if (d > 0) {
	  e.cap -= d;
	  graph[e.to][e.rev].cap += d;
	  return d;
	}
      }
    }
    return 0;
  }
public:
  /* v is the number of vertices (labeled from 0 .. v-1) */
  Dinic(int v) : graph(v), level(v, -1), iter(v, 0) {}
  void add_edge(int from, int to, T cap) {
    graph[from].push_back((edge) {to, cap, graph[to].size()});
    graph[to].push_back((edge) {from, 0, graph[from].size() - 1});
  }
  T max_flow(int s, int t) {
    T flow = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0) {
	return flow;
      }
      iter.assign(iter.size(), 0);
      T f;
      while ((f = dfs(s, t, -1)) > 0) {
	flow += f;
      }
    }
  }
  std::pair<T,std::vector<int> > max_flow_cut(int s, int t) {
    T flow = 0;
    while (1) {
      bfs(s);
      if (level[t] < 0) {
	std::vector<int> ret;
	for (int i = 0; i < graph.size(); ++i) {
	  if (level[i] < 0) {
	    ret.push_back(i);
	  }
	}
	return std::pair<T, std::vector<int> >(flow, ret);
      }
      iter.assign(iter.size(), 0);
      T f;
      while ((f = dfs(s, t, -1)) > 0) {
	flow += f;
      }
    }
  }
};


int nv;

static void add(Dinic<lint> &g,int s,int t,int cap){
  assert (s<nv);
  assert (t<nv);
  g.add_edge(s,t,cap);
}



int main(){
  int h,w;
  cin>>h>>w;
  vector<string>s(h);
  rep(i,h)cin>>s[i];
  nv=2+h+w+2*h*w;
  Dinic<lint> din(nv);
  //row/col->vertex
  int I=10000000;
  for(int i=0;i<h;++i)
    for(int j=0;j<w;++j){
      int src=2+i;
      int dst=2+h+w+2*(i*w+j);
      add(din,src,dst,I);
    }
  for(int i=0;i<h;++i)
    for(int j=0;j<w;++j){
      int src=2+h+j;
      int dst=2+h+w+2*(i*w+j);
      add(din,src,dst,I);
    }
  // vertex->row/col
  for(int i=0;i<h;++i)
    for(int j=0;j<w;++j){
      int src=2+h+w+2*(i*w+j)+1;
      int dst=2+i;
      add(din,src,dst,I);
    }
  for(int i=0;i<h;++i)
    for(int j=0;j<w;++j){
      int src=2+h+w+2*(i*w+j)+1;
      int dst=2+h+j;
      add(din,src,dst,I);
    }
  //vertex
  int sv=-1,tv=-1;
  for(int i=0;i<h;++i)
    for(int j=0;j<w;++j){
      if(s[i][j]=='S')sv=2+h+w+2*(i*w+j)+1;
      if(s[i][j]=='T')tv=2+h+w+2*(i*w+j)+1;
    }
  for(int i=0;i<h;++i)
    for(int j=0;j<w;++j){
      if(s[i][j]=='.')continue;
      int src=2+h+w+2*(i*w+j);
      int dst=2+h+w+2*(i*w+j)+1;
      if(src==sv||dst==tv)add(din,src,dst,I);
      else add(din,src,dst,1);
    }
  // s->S
  add(din,0,sv,I);
  // T->t
  add(din,tv,1,I);
  lint minCut=din.max_flow(0,1);
  cout<<(minCut>h*w?-1:minCut)<<"\n";
}


Measured memory_bytes: 4352