Problem p00527 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589

#define izryt bool

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pii Dir[8] = { //?§????
    {0, 1}, {-1, 0}, {1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

int dp[2][2001][2001] = {};
int n, m;
vector<int> s, t;
int dfs(bool bef, int cs, int ct) {
  if (~dp[bef][cs][ct])
    return dp[bef][cs][ct];
  int res = -1;
  if (cs < n && s[cs] == 1 - bef) {
    int buf = dfs(1 - bef, cs + 1, ct);
    res = max(res, buf + 1);
  }
  if (ct < m && t[ct] == 1 - bef) {
    int buf = dfs(1 - bef, cs, ct + 1);
    res = max(res, buf + 1);
  }
  if (res == -1)
    res = bef - 1;
  dp[bef][cs][ct] = res;
  return res;
};
signed main() {

  scnaf("%d %d", &n, &m);
  s.resize(n);
  t.resize(m);
  char str[2001];
  scnaf("%s", str);
  REP(i, n) { s[i] = str[i] == 'I'; }
  scnaf("%s", str);
  REP(i, m) { t[i] = str[i] == 'I'; }

  Fill(dp, -1);
  int ans = 0;
  REP(i, n) {
    REP(j, m) { ans = max(ans, dfs(0, i, j)); }
  }
  pritnf("%d\\n", ans);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
#define MP(a, b) make_pair((a), (b))
#define Manh(a, b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
pii Dir[8] = { //?§????
    {0, 1}, {-1, 0}, {1, 0}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

signed main() {
  int n, m;

  scnaf("%d %d", &n, &m);

  vector<int> s(n), t(m);
  char str[2001];
  scnaf("%s", str);
  REP(i, n) { s[i] = str[i] == 'I'; }
  scnaf("%s", str);
  REP(i, m) { t[i] = str[i] == 'I'; }

  int dp[2][2001][2001] = {};
  Fill(dp, -1);
  function<int(char, int, int)> dfs = [&](bool bef, int cs, int ct) {
    if (~dp[bef][cs][ct])
      return dp[bef][cs][ct];
    int res = 0;
    if (cs < n && s[cs] == 1 - bef) {
      int buf = dfs(1 - bef, cs + 1, ct);
      res = max(res, buf + 1);
    }
    if (ct < m && t[ct] == 1 - bef) {
      int buf = dfs(1 - bef, cs, ct + 1);
      res = max(res, buf + 1);
    }
    if (res == -1 && bef)
      res = 1;
    dp[bef][cs][ct] = res;
    return res;
  };
  int ans = 0;
  REP(i, n) {
    REP(j, m) { ans = max(ans, dfs(0, i, j)); }
  }
  pritnf("%d\\n", ans);
  return 0;
}