Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define re(i, a, b) for (int i = (a); i < (b); i++)
#define repd(i, a, b) for (int i = (a); i >= (b); i--)
#define run(a) for (int k = head[a]; k; k = e[k].ne)
#define v e[k].t
#define clr(a) memset(a, 0, sizeof(a));
#define il inline
#define sz(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define mp make_pair
#define pb push_back
#define w1 first
#define w2 second
#define adm(a, b, c)                                                           \
  {                                                                            \
    a = a + b;                                                                 \
    if (a >= c)                                                                \
      a -= c;                                                                  \
    else if (a < 0)                                                            \
      a += c;                                                                  \
  }
typedef double db;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> pa;
const int N = 5e3 + 5, M = 5e3 + 5, INF = 1e9, mod = 998244353;
const ll linf = 1e18;
const double eps = 1e-8, pi = acos(-1);
il int gmin(int &a, int b) {
  if (a > b)
    a = b;
}
il ll gmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
il int gmax(int &a, int b) {
  if (a < b)
    a = b;
}
il ll gmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
il void read(ll &x) {
  ll f = 1, t = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    t = t * 10 + ch - '0';
    ch = getchar();
  }
  x = t * f;
}
il ll read(ll &x, ll &y) {
  read(x);
  read(y);
}
il void read(int &x) {
  int f = 1, t = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    t = t * 10 + ch - '0';
    ch = getchar();
  }
  x = t * f;
}
il int read(int &x, int &y) {
  read(x);
  read(y);
}
il void read(int &a, int &b, int &c) {
  read(a);
  read(b);
  read(c);
}
il void read(ll &a, ll &b, ll &c) {
  read(a);
  read(b);
  read(c);
}
il int read() {
  int x;
  read(x);
  return x;
}
il ll qpow(ll a, ll b, ll p) {
  ll ret = 1;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1)
      ret = ret * a % p;
  return ret;
}
il ll qpow(ll a, ll b) {
  ll ret = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ret = ret * a % mod;
  return ret;
}
il ll qmul(ll a, ll b, ll p) {
  ll ret = 0;
  for (; b; b >>= 1, a = (a << 1) % p)
    if (b & 1)
      adm(ret, a, p);
  return ret;
}
il ll qmul(ll a, ll b) {
  ll ret = 0;
  for (; b; b >>= 1, a = (a << 1) % mod)
    if (b & 1)
      adm(ret, a, mod);
  return ret;
}
il void judge() {
  freopen("data.in", "r", stdin);
  freopen("data.out", "w", stdout);
}
int n, m, S, T, cnt = 1;
char s[N];
int head[N];
struct edge {
  int t, ne, c;
} e[N];
void adde(int a, int b, int c) {
  e[++cnt] = (edge){b, head[a], c};
  head[a] = cnt;
  e[++cnt] = (edge){a, head[b], 0};
  head[b] = cnt;
}
int d[N], w[N], cur[N], pre[N], prek[N], has[N];
int isap(int S, int T, int n) {
  int x = S, flag, ans = 0, flow = INF;
  while (d[S] <= n) {
    flag = 0;
    w[x] = flow;
    for (int k = cur[x]; k; k = e[k].ne)
      if (e[k].c && d[v] + 1 == d[x]) {
        flag = 1;
        gmin(flow, e[k].c);
        pre[v] = x;
        prek[v] = cur[x] = k;
        x = v;
        if (x == T) {
          while (x != S) {
            int t = prek[x];
            e[t].c -= flow;
            e[t ^ 1].c += flow;
            x = pre[x];
          }
          ans += flow;
          flow = INF;
        }
        break;
      }
    if (flag)
      continue;
    int mnd = n;
    run(x) if (e[k].c && d[v] < mnd) cur[x] = k, mnd = d[v];
    has[d[x]]--;
    if (!has[d[x]])
      break;
    has[d[x] = mnd + 1]++;
    if (x != S) {
      x = pre[x];
      flow = w[x];
    }
  }
  return ans;
}
int main() {
  read(n, m);
  S = n + m + 1, T = S + 1;
  rep(i, 1, n) {
    scanf("%s", s + 1);
    rep(j, 1, m) {
      if (s[j] == 'S')
        adde(S, i, INF), adde(S, j + n, INF);
      if (s[j] == 'T')
        adde(i, T, INF), adde(j + n, T, INF);
      if (s[j] == 'o')
        adde(i, j + n, 1), adde(j + n, i, 1);
    }
  }
  int ans = isap(S, T, T);
  if (ans >= INF)
    ans = -1;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define re(i, a, b) for (int i = (a); i < (b); i++)
#define repd(i, a, b) for (int i = (a); i >= (b); i--)
#define run(a) for (int k = head[a]; k; k = e[k].ne)
#define v e[k].t
#define clr(a) memset(a, 0, sizeof(a));
#define il inline
#define sz(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define mp make_pair
#define pb push_back
#define w1 first
#define w2 second
#define adm(a, b, c)                                                           \
  {                                                                            \
    a = a + b;                                                                 \
    if (a >= c)                                                                \
      a -= c;                                                                  \
    else if (a < 0)                                                            \
      a += c;                                                                  \
  }
typedef double db;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> pa;
const int N = 3e6 + 5, M = 5e3 + 5, INF = 1e9, mod = 998244353;
const ll linf = 1e18;
const double eps = 1e-8, pi = acos(-1);
il int gmin(int &a, int b) {
  if (a > b)
    a = b;
}
il ll gmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
il int gmax(int &a, int b) {
  if (a < b)
    a = b;
}
il ll gmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
il void read(ll &x) {
  ll f = 1, t = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    t = t * 10 + ch - '0';
    ch = getchar();
  }
  x = t * f;
}
il ll read(ll &x, ll &y) {
  read(x);
  read(y);
}
il void read(int &x) {
  int f = 1, t = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    t = t * 10 + ch - '0';
    ch = getchar();
  }
  x = t * f;
}
il int read(int &x, int &y) {
  read(x);
  read(y);
}
il void read(int &a, int &b, int &c) {
  read(a);
  read(b);
  read(c);
}
il void read(ll &a, ll &b, ll &c) {
  read(a);
  read(b);
  read(c);
}
il int read() {
  int x;
  read(x);
  return x;
}
il ll qpow(ll a, ll b, ll p) {
  ll ret = 1;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1)
      ret = ret * a % p;
  return ret;
}
il ll qpow(ll a, ll b) {
  ll ret = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ret = ret * a % mod;
  return ret;
}
il ll qmul(ll a, ll b, ll p) {
  ll ret = 0;
  for (; b; b >>= 1, a = (a << 1) % p)
    if (b & 1)
      adm(ret, a, p);
  return ret;
}
il ll qmul(ll a, ll b) {
  ll ret = 0;
  for (; b; b >>= 1, a = (a << 1) % mod)
    if (b & 1)
      adm(ret, a, mod);
  return ret;
}
il void judge() {
  freopen("data.in", "r", stdin);
  freopen("data.out", "w", stdout);
}
int n, m, S, T, cnt = 1;
char s[N];
int head[N];
struct edge {
  int t, ne, c;
} e[N];
void adde(int a, int b, int c) {
  e[++cnt] = (edge){b, head[a], c};
  head[a] = cnt;
  e[++cnt] = (edge){a, head[b], 0};
  head[b] = cnt;
}
int d[N], w[N], cur[N], pre[N], prek[N], has[N];
int isap(int S, int T, int n) {
  int x = S, flag, ans = 0, flow = INF;
  while (d[S] <= n) {
    flag = 0;
    w[x] = flow;
    for (int k = cur[x]; k; k = e[k].ne)
      if (e[k].c && d[v] + 1 == d[x]) {
        flag = 1;
        gmin(flow, e[k].c);
        pre[v] = x;
        prek[v] = cur[x] = k;
        x = v;
        if (x == T) {
          while (x != S) {
            int t = prek[x];
            e[t].c -= flow;
            e[t ^ 1].c += flow;
            x = pre[x];
          }
          ans += flow;
          flow = INF;
        }
        break;
      }
    if (flag)
      continue;
    int mnd = n;
    run(x) if (e[k].c && d[v] < mnd) cur[x] = k, mnd = d[v];
    has[d[x]]--;
    if (!has[d[x]])
      break;
    has[d[x] = mnd + 1]++;
    if (x != S) {
      x = pre[x];
      flow = w[x];
    }
  }
  return ans;
}
int main() {
  read(n, m);
  S = n + m + 1, T = S + 1;
  rep(i, 1, n) {
    scanf("%s", s + 1);
    rep(j, 1, m) {
      if (s[j] == 'S')
        adde(S, i, INF), adde(S, j + n, INF);
      if (s[j] == 'T')
        adde(i, T, INF), adde(j + n, T, INF);
      if (s[j] == 'o')
        adde(i, j + n, 1), adde(j + n, i, 1);
    }
  }
  int ans = isap(S, T, T);
  if (ans >= INF)
    ans = -1;
  cout << ans << endl;
}