Language: C++

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

int H, W;
char field[105][105];

class Dinic {
  int MAX_V;
  int INF;
  struct edge{ int to, cap, rev, icap, flow; };

  vector< vector<edge> > G;
  vector<int> level; //sからの距離
  vector<int> iter; //どこまで調べたか

  void max_flow_bfs(int s){
    fill(level.begin(), level.end(), -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while(!que.empty()){
      int v = que.front(); que.pop();
      for(int i=0; i<G[v].size(); i++){
        edge &e = G[v][i];
        if(e.cap>0 && level[e.to]<0){
          level[e.to] = level[v] + 1;
          que.push(e.to);
        }
      }
    }
  }
  int max_flow_dfs(int v, int t, int f){
    if(v==t) return f;
    for(int &i=iter[v]; i<G[v].size(); i++){
      edge &e = G[v][i];
      if(e.cap>0 && level[v]<level[e.to]){
        int d = max_flow_dfs(e.to, t, min(f, e.cap));
        if(d>0){
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          e.flow += d;
          return d;
        }
      }
    }
    return 0;
  }

public:
  Dinic(int N):MAX_V(N),G(N),level(N),iter(N){
    INF = 99999999;
  }

  void add_edge(int from, int to, int cap){
    G[from].push_back((edge){to, cap, G[to].size(), cap, 0});
    G[to].push_back((edge){from, 0, G[from].size()-1, 0, 0});
  }

  int get_flow(int from, int to){ //untried
    rep(i,G[from].size()){
      if(G[from][i].to == to){
        return G[from][i].flow;
      }
    }
    return -1;
  }

  int max_flow(int s, int t){
    int flow = 0;
    while(true){
      max_flow_bfs(s);
      if(level[t]<0) return flow;
      fill(iter.begin(), iter.end(), 0);
      int f;
      while((f = max_flow_dfs(s, t, INF))>0){
        flow += f;
      }
    }
  }
  
  int min_cut(int s, int t, vector<int>& S, vector<int>& T){
    S.clear();
    T.clear();

    int maxf = max_flow(s, t);
    for(int i=0; i<level.size(); i++){
      if(level[i] >= 0) S.push_back(i);
      else T.push_back(i);
    }

    return maxf;
  }
};

int main(){
  cin >> H >> W;
  rep(i,H){
    string str;
    cin >> str;
    rep(j,W){
      field[i][j] = str[j];
    }
  }

  Dinic dinic(1005);
  int s_id = -1;
  int t_id = -1;

  rep(i,H){
    rep(j,W){
      if(field[i][j] == 'S'){
        s_id = i*W+j;
        field[i][j] = 'o';
      }
      if(field[i][j] == 'T'){
        t_id = i*W+j;
        field[i][j] = 'o';
      }
    }
  }

  int sy = s_id/W, sx = s_id%W;
  int ty = t_id/W, tx = t_id%W;
  if(sy == ty || sx == tx){
    cout << -1 << endl;
    return 0;
  }

  rep(i,H){
    rep(j,W){
      if(field[i][j] == 'o'){
        rep(k,W){
          if(k!=j && field[i][k] == 'o'){
            dinic.add_edge(i*W+j, i*W+k);
            dinic.add_edge(i*W+k, i*W+j);
          }
        }
        rep(k,H){
          if(k!=i && field[k][j] == 'o'){
            dinic.add_edge(i*W+j, k*W+j);
            dinic.add_edge(k*W+j, i*W+j);
          }
        }
      }
    }
  }

  vector<int> S, T;
  cout << dinic.min_cut(s_id, t_id, S, T) << endl;
  
  return 0;
}


Compiler output:
a.cc: In member function 'void Dinic::add_edge(int, int, int)':
a.cc:61:49: warning: narrowing conversion of '(&((Dinic*)this)->Dinic::G.std::vector<std::vector<Dinic::edge> >::operator[](((std::vector<std::vector<Dinic::edge> >::size_type)to)))->std::vector<Dinic::edge>::size()' from 'std::vector<Dinic::edge>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
   61 |     G[from].push_back((edge){to, cap, G[to].size(), cap, 0});
      |                                       ~~~~~~~~~~^~
a.cc:62:51: warning: narrowing conversion of '((&((Dinic*)this)->Dinic::G.std::vector<std::vector<Dinic::edge> >::operator[](((std::vector<std::vector<Dinic::edge> >::size_type)from)))->std::vector<Dinic::edge>::size() - 1)' from 'std::vector<Dinic::edge>::size_type' {aka 'long unsigned int'} to 'int' [-Wnarrowing]
   62 |     G[to].push_back((edge){from, 0, G[from].size()-1, 0, 0});
      |                                     ~~~~~~~~~~~~~~^~
a.cc: In function 'int main()':
a.cc:140:27: error: no matching function for call to 'Dinic::add_edge(int, int)'
  140 |             dinic.add_edge(i*W+j, i*W+k);
      |             ~~~~~~~~~~~~~~^~~~~~~~~~~~~~
a.cc:60:8: note: candidate: 'void Dinic::add_edge(int, int, int)'
   60 |   void add_edge(int from, int to, int cap){
      |        ^~~~~~~~
a.cc:60:8: note:   candidate expects 3 arguments, 2 provided
a.cc:141:27: error: no matching function for call to 'Dinic::add_edge(int, int)'
  141 |             dinic.add_edge(i*W+k, i*W+j);
      |             ~~~~~~~~~~~~~~^~~~~~~~~~~~~~
a.cc:60:8: note: candidate: 'void Dinic::add_edge(int, int, int)'
   60 |   void add_edge(int from, int to, int cap){
      |        ^~~~~~~~
a.cc:60:8: note:   candidate expects 3 arguments, 2 provided
a.cc:146:27: error: no matching function for call to 'Dinic::add_edge(int, int)'
  146 |             dinic.add_edge(i*W+j, k*W+j);
      |             ~~~~~~~~~~~~~~^~~~~~~~~~~~~~
a.cc:60:8: note: candidate: 'void Dinic::add_edge(int, int, int)'
   60 |   void add_edge(int from, int to, int cap){
      |        ^~~~~~~~
a.cc:60:8: note:   candidate expects 3 arguments, 2 provided
a.cc:147:27: error: no matching function for call to 'Dinic::add_edge(int, int)'
  147 |             dinic.add_edge(k*W+j, i*W+j);
      |             ~~~~~~~~~~~~~~^~~~~~~~~~~~~~
a.cc:60:8: note: candidate: 'void Dinic::add_edge(int, int, int)'
   60 |   void add_edge(int from, int to, int cap){
      |        ^~~~~~~~
a.cc:60:8: note:   candidate expects 3 arguments, 2 provided