Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, m, S, T, cnt, inf = 1e9, sx, sy, tx, ty, hed[205], d[205], nex[10005],
                     to[10005], val[10005], ans;
queue<int> q;
char ch;
void add(int x, int y, int z) {
  to[cnt] = y;
  val[cnt] = z;
  nex[cnt] = hed[x];
  hed[x] = cnt++;
  to[cnt] = x;
  val[cnt] = 0;
  nex[cnt] = hed[y];
  hed[y] = cnt++;
}
bool bfs() {
  while (!q.empty())
    q.pop();
  memset(d, 0, sizeof(d));
  d[S] = 1;
  q.push(S);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = hed[x]; i != -1; i = nex[i])
      if (!d[to[i]] && val[i]) {
        d[to[i]] = d[x] + 1;
        if (to[i] == T)
          return 1;
        q.push(to[i]);
      }
  }
  return 0;
}
int dfs(int x, int mf) {
  if (x == T)
    return mf;
  int tmp = mf;
  for (int i = hed[x]; i != -1; i = nex[i])
    if (d[to[i]] == d[x] + 1 && val[i]) {
      int k = dfs(to[i], min(val[i], tmp));
      if (!k)
        d[to[i]] = 0;
      tmp -= k;
      val[i] -= k;
      val[i ^ 1] += k;
      if (!tmp)
        break;
    }
  return mf - tmp;
}
int main() {
  scanf("%d%d", &n, &m);
  T = n + m + 1;
  memset(hed, -1, sizeof(hed));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> ch;
      if (ch == 'S')
        add(S, i, inf), add(S, j + n, inf), sx = i, sy = j;
      if (ch == 'T')
        add(i, T, inf), add(j + n, T, inf), tx = i, ty = j;
      if (ch == 'o')
        add(i, j + n, 1), add(j + n, i, 1);
    }
  if (sx == tx || sy == ty)
    return puts("-1"), 0;
  while (bfs())
    ans += dfs(0, inf * 2);
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, S, T, cnt, inf = 1e9, sx, sy, tx, ty, hed[1005], d[1005], nex[50005],
                     to[50005], val[50005], ans;
queue<int> q;
char ch;
void add(int x, int y, int z) {
  to[cnt] = y;
  val[cnt] = z;
  nex[cnt] = hed[x];
  hed[x] = cnt++;
  to[cnt] = x;
  val[cnt] = 0;
  nex[cnt] = hed[y];
  hed[y] = cnt++;
}
bool bfs() {
  while (!q.empty())
    q.pop();
  memset(d, 0, sizeof(d));
  d[S] = 1;
  q.push(S);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = hed[x]; i != -1; i = nex[i])
      if (!d[to[i]] && val[i]) {
        d[to[i]] = d[x] + 1;
        if (to[i] == T)
          return 1;
        q.push(to[i]);
      }
  }
  return 0;
}
int dfs(int x, int mf) {
  if (x == T)
    return mf;
  int tmp = mf;
  for (int i = hed[x]; i != -1; i = nex[i])
    if (d[to[i]] == d[x] + 1 && val[i]) {
      int k = dfs(to[i], min(val[i], tmp));
      if (!k)
        d[to[i]] = 0;
      tmp -= k;
      val[i] -= k;
      val[i ^ 1] += k;
      if (!tmp)
        break;
    }
  return mf - tmp;
}
int main() {
  scanf("%d%d", &n, &m);
  T = n + m + 1;
  memset(hed, -1, sizeof(hed));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> ch;
      if (ch == 'S')
        add(S, i, inf), add(S, j + n, inf), sx = i, sy = j;
      if (ch == 'T')
        add(i, T, inf), add(j + n, T, inf), tx = i, ty = j;
      if (ch == 'o')
        add(i, j + n, 1), add(j + n, i, 1);
    }
  if (sx == tx || sy == ty)
    return puts("-1"), 0;
  while (bfs())
    ans += dfs(0, inf * 2);
  printf("%d\n", ans);
}