Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int N = 10050, inf = 1e9;
using namespace std;
inline int gi() {
  char ch = getchar();
  int x = 0, q = 0;
  while (ch < '0' || ch > '9')
    ch == '-' ? q = 1 : 0, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return q ? (-x) : x;
}
inline int gc() {
  char ch = getchar();
  while (ch != 'S' && ch != 'o' && ch != '.' && ch != 'T')
    ch = getchar();
  if (ch == '.')
    return 0;
  if (ch == 'o')
    return 1;
  if (ch == 'S')
    return 2;
  return 3;
}
int S, T, nn[N << 1][3], head[N], cnt = 1, tot, vis[N], st[N];
inline void link(int u, int v, int w) {
  nn[++cnt][1] = v, nn[cnt][0] = head[u], head[u] = cnt, nn[cnt][2] = w;
  nn[++cnt][1] = u, nn[cnt][0] = head[v], head[v] = cnt, nn[cnt][2] = 0;
  return;
}
inline int bfs() {
  for (int i = 1; i <= T; ++i)
    vis[i] = 0;
  int l = 0, r = 1, s, t;
  st[1] = S, vis[S] = 1;
  while (l < r) {
    s = st[++l];
    for (int i = head[s]; i; i = nn[i][0])
      if (nn[i][2] && !vis[t = nn[i][1]])
        vis[t] = vis[s] + 1, st[++r] = t;
    if (vis[T])
      return 1;
  }
  return 0;
}
inline int Min(int a, int b) { return a < b ? a : b; }
int dinic(int x, int flow) {
  if (x == T)
    return flow;
  int tmp, s = 0, t;
  for (int i = head[x]; i; i = nn[i][0])
    if (nn[i][2] && vis[t = nn[i][1]] == vis[x] + 1) {
      tmp = dinic(t, Min(flow, nn[i][2]));
      s += tmp, nn[i][2] -= tmp, nn[i ^ 1][2] += tmp, flow -= tmp;
      if (!flow)
        break;
    }
  if (!s)
    vis[x] = 0;
  return s;
}
int main() {
  int h = gi(), w = gi(), s;
  tot = h + w + 2;
  S = tot - 1, T = tot;
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j) {
      s = gc();
      if (!s)
        continue;
      if (s == 1)
        link(i, j + h, 1), link(j + h, i, 1);
      if (s == 2)
        link(S, i, inf), link(S, j + h, inf), link(i, j + h, inf),
            link(j + h, i, inf);
      if (s == 3)
        link(j + h, T, inf), link(i, T, inf), link(i, j + h, inf),
            link(j + h, i, inf);
    }
  int ans = 0;
  while (bfs())
    ans += dinic(S, inf);
  if (ans >= inf)
    puts("-1");
  else
    printf("%d", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
const int N = 100050, inf = 1e9;
using namespace std;
inline int gi() {
  char ch = getchar();
  int x = 0, q = 0;
  while (ch < '0' || ch > '9')
    ch == '-' ? q = 1 : 0, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return q ? (-x) : x;
}
inline int gc() {
  char ch = getchar();
  while (ch != 'S' && ch != 'o' && ch != '.' && ch != 'T')
    ch = getchar();
  if (ch == '.')
    return 0;
  if (ch == 'o')
    return 1;
  if (ch == 'S')
    return 2;
  return 3;
}
int S, T, nn[N << 1][3], head[N], cnt = 1, tot, vis[N], st[N];
inline void link(int u, int v, int w) {
  nn[++cnt][1] = v, nn[cnt][0] = head[u], head[u] = cnt, nn[cnt][2] = w;
  nn[++cnt][1] = u, nn[cnt][0] = head[v], head[v] = cnt, nn[cnt][2] = 0;
  return;
}
inline int bfs() {
  for (int i = 1; i <= T; ++i)
    vis[i] = 0;
  int l = 0, r = 1, s, t;
  st[1] = S, vis[S] = 1;
  while (l < r) {
    s = st[++l];
    for (int i = head[s]; i; i = nn[i][0])
      if (nn[i][2] && !vis[t = nn[i][1]])
        vis[t] = vis[s] + 1, st[++r] = t;
    if (vis[T])
      return 1;
  }
  return 0;
}
inline int Min(int a, int b) { return a < b ? a : b; }
int dinic(int x, int flow) {
  if (x == T)
    return flow;
  int tmp, s = 0, t;
  for (int i = head[x]; i; i = nn[i][0])
    if (nn[i][2] && vis[t = nn[i][1]] == vis[x] + 1) {
      tmp = dinic(t, Min(flow, nn[i][2]));
      s += tmp, nn[i][2] -= tmp, nn[i ^ 1][2] += tmp, flow -= tmp;
      if (!flow)
        break;
    }
  if (!s)
    vis[x] = 0;
  return s;
}
int main() {
  int h = gi(), w = gi(), s;
  tot = h + w + 2;
  S = tot - 1, T = tot;
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j) {
      s = gc();
      if (!s)
        continue;
      if (s == 1)
        link(i, j + h, 1), link(j + h, i, 1);
      if (s == 2)
        link(S, i, inf), link(S, j + h, inf), link(i, j + h, inf),
            link(j + h, i, inf);
      if (s == 3)
        link(j + h, T, inf), link(i, T, inf), link(i, j + h, inf),
            link(j + h, i, inf);
    }
  int ans = 0;
  while (bfs())
    ans += dinic(S, inf);
  if (ans >= inf)
    puts("-1");
  else
    printf("%d", ans);
  return 0;
}