Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(i, a, b) for (i = a; i <= b; i++)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 5, inf = 1e8;
int i, j, n, num, H, W, S, T, last;
int k[maxn], ne[maxn], g[maxn], f[maxn], d[maxn];
int bz[maxn], bk, ans, now[maxn];
char c[105][105];
void add(int x, int y, int z) {
  ne[++num] = k[x], k[x] = num, g[num] = y, f[num] = z;
  ne[++num] = k[y], k[y] = num, g[num] = x;
}
int dfs(int x, int a) {
  if (x == T) {
    ans += a;
    return a;
  }
  bz[x] = bk;
  int i;
  for (i = k[x]; i; i = ne[i]) {
    int y = g[i];
    if (f[i] && bz[y] != bk && d[x] == d[y] + 1) {
      int j = dfs(y, min(a, f[i]));
      if (j) {
        f[i] -= j, f[i ^ 1] += j, now[x] = i;
        return j;
      }
    }
  }
  return now[x] = 0;
}
bool update() {
  int z = inf, i, x, y;
  fo(x, 1, n) if (bz[x] == bk) {
    for (i = k[x]; i; i = ne[i]) {
      y = g[i];
      if (f[i] && bz[y] != bk)
        z = min(z, d[y] + 1 - d[x]);
    }
  }
  if (z == inf)
    return 0;
  fo(x, 1, n) if (bz[x] == bk) d[x] += z;
  return 1;
}
int main() {
  scanf("%d%d", &H, &W);
  fo(i, 1, H) {
    scanf("%s", c[i] + 1);
    fo(j, 1, W) if (c[i][j] != '.') n++;
  }
  n += H + W;
  num = 1;
  fo(i, 1, H) last++, add(last, last + n, inf);
  fo(i, 1, W) last++, add(last, last + n, inf);
  fo(i, 1, H) {
    fo(j, 1, W) {
      if (c[i][j] != '.') {
        last++, add(last, last + n, c[i][j] == 'o' ? 1 : inf);
        add(last + n, i, inf), add(i + n, last, inf);
        add(last + n, j + H, inf), add(j + H + n, last, inf);
        if (c[i][j] == 'S')
          S = last;
        if (c[i][j] == 'T')
          T = last + n;
      }
    }
  }
  n <<= 1;
  for (;;) {
    fo(i, 1, n) now[i] = k[i];
    bk++;
    for (; dfs(S, inf); bk++)
      ;
    if (ans >= inf || !update())
      break;
  }
  if (ans >= inf)
    puts("-1");
  else
    printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (i = a; i <= b; i++)
#define fd(i, a, b) for (i = a; i >= b; i--)
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
typedef long long ll;
using namespace std;
const int maxn = 2e5 + 5, inf = 1e8;
int i, j, n, num, H, W, S, T, last;
int k[maxn], ne[maxn], g[maxn], f[maxn], d[maxn];
int bz[maxn], bk, ans, now[maxn];
char c[105][105];
void add(int x, int y, int z) {
  ne[++num] = k[x], k[x] = num, g[num] = y, f[num] = z;
  ne[++num] = k[y], k[y] = num, g[num] = x;
}
int dfs(int x, int a) {
  if (x == T) {
    ans += a;
    return a;
  }
  bz[x] = bk;
  int i;
  for (i = k[x]; i; i = ne[i]) {
    int y = g[i];
    if (f[i] && bz[y] != bk && d[x] == d[y] + 1) {
      int j = dfs(y, min(a, f[i]));
      if (j) {
        f[i] -= j, f[i ^ 1] += j, now[x] = i;
        return j;
      }
    }
  }
  return now[x] = 0;
}
bool update() {
  int z = inf, i, x, y;
  fo(x, 1, n) if (bz[x] == bk) {
    for (i = k[x]; i; i = ne[i]) {
      y = g[i];
      if (f[i] && bz[y] != bk)
        z = min(z, d[y] + 1 - d[x]);
    }
  }
  if (z == inf)
    return 0;
  fo(x, 1, n) if (bz[x] == bk) d[x] += z;
  return 1;
}
int main() {
  scanf("%d%d", &H, &W);
  fo(i, 1, H) {
    scanf("%s", c[i] + 1);
    fo(j, 1, W) if (c[i][j] != '.') n++;
  }
  n += H + W;
  num = 1;
  fo(i, 1, H) last++, add(last, last + n, inf);
  fo(i, 1, W) last++, add(last, last + n, inf);
  fo(i, 1, H) {
    fo(j, 1, W) {
      if (c[i][j] != '.') {
        last++, add(last, last + n, c[i][j] == 'o' ? 1 : inf);
        add(last + n, i, inf), add(i + n, last, inf);
        add(last + n, j + H, inf), add(j + H + n, last, inf);
        if (c[i][j] == 'S')
          S = last;
        if (c[i][j] == 'T')
          T = last + n;
      }
    }
  }
  n <<= 1;
  for (;;) {
    fo(i, 1, n) now[i] = k[i];
    bk++;
    for (; dfs(S, inf); bk++)
      ;
    if (ans >= inf || !update())
      break;
  }
  if (ans >= inf)
    puts("-1");
  else
    printf("%d\n", ans);
}