Problem p03718 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int n, m;
string s[100];
int si, sj, fi, fj;
int ans;
int sz;
vector<int> graph[500];
int c[12000];
int f[12000];
int v[12000];
int u[12000];

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'S')
        si = i, sj = j;
      if (s[i][j] == 'T')
        fi = i, fj = j;
    }
  }
  if (si == fi || sj == fj)
    cout << -1, exit(0);
  if (s[si][fj] == 'o')
    ans++, s[si][fj] = '.';
  if (s[fi][sj] == 'o')
    ans++, s[fi][sj] = '.';
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] != 'o')
        continue;
      v[sz] = i;
      u[sz] = j + n;
      c[sz] = (j != sj && i != fi);
      graph[i].push_back(sz);
      sz++;
      v[sz] = i;
      u[sz] = j + n;
      c[sz] = (i != si && j != fj);
      graph[j + n].push_back(sz);
      sz++;
    }
  }
  while (1) {
    vector<int> prv(200, -1);
    vector<int> vec;
    vec.push_back(si);
    vec.push_back(sj + n);
    prv[si] = -2, prv[sj + n] = -2;
    for (int i = 0; i < vec.size(); i++) {
      int vert = vec[i];
      for (int j = 0; j < graph[vert].size(); j++) {
        int id = graph[vert][j];
        int coct = v[id] + u[id] - vert;
        if (f[id] < c[id] && prv[coct] == -1) {
          vec.push_back(coct);
          prv[coct] = id;
        }
      }
    }
    if (prv[fi] == -1 && prv[fj + n] == -1) {
      cout << ans;
      return 0;
    }
    ans++;
    if (prv[fi] != -1) {
      int we = fi;
      while (1) {
        int id = prv[we];
        f[id]++;
        f[id ^ 1]--;
        we = u[id] + v[id] - we;
        if (prv[we] == -2)
          break;
      }
    } else {
      int we = fj + n;
      while (1) {
        int id = prv[we];
        f[id]++;
        f[id ^ 1]--;
        we = u[id] + v[id] - we;
        if (prv[we] == -2)
          break;
      }
    }
  }
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int n, m;
string s[100];
int si, sj, fi, fj;
int ans;
int sz;
vector<int> graph[500];
int c[22000];
int f[22000];
int v[22000];
int u[22000];

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'S')
        si = i, sj = j;
      if (s[i][j] == 'T')
        fi = i, fj = j;
    }
  }
  if (si == fi || sj == fj)
    cout << -1, exit(0);
  if (s[si][fj] == 'o')
    ans++, s[si][fj] = '.';
  if (s[fi][sj] == 'o')
    ans++, s[fi][sj] = '.';
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] != 'o')
        continue;
      v[sz] = i;
      u[sz] = j + n;
      c[sz] = (j != sj && i != fi);
      graph[i].push_back(sz);
      sz++;
      v[sz] = i;
      u[sz] = j + n;
      c[sz] = (i != si && j != fj);
      graph[j + n].push_back(sz);
      sz++;
    }
  }
  while (1) {
    vector<int> prv(200, -1);
    vector<int> vec;
    vec.push_back(si);
    vec.push_back(sj + n);
    prv[si] = -2, prv[sj + n] = -2;
    for (int i = 0; i < vec.size(); i++) {
      int vert = vec[i];
      for (int j = 0; j < graph[vert].size(); j++) {
        int id = graph[vert][j];
        int coct = v[id] + u[id] - vert;
        if (f[id] < c[id] && prv[coct] == -1) {
          vec.push_back(coct);
          prv[coct] = id;
        }
      }
    }
    if (prv[fi] == -1 && prv[fj + n] == -1) {
      cout << ans;
      return 0;
    }
    ans++;
    if (prv[fi] != -1) {
      int we = fi;
      while (1) {
        int id = prv[we];
        f[id]++;
        f[id ^ 1]--;
        we = u[id] + v[id] - we;
        if (prv[we] == -2)
          break;
      }
    } else {
      int we = fj + n;
      while (1) {
        int id = prv[we];
        f[id]++;
        f[id ^ 1]--;
        we = u[id] + v[id] - we;
        if (prv[we] == -2)
          break;
      }
    }
  }
}