Problem p03707 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// #define CHKR
#define ll long long
#define pb push_back
// #define mp make_pair
#define fr first
#define sc second
#define ARRS int(2e5 + 500)
#define BARRS int(6e4 + 600)
#define MAX ((long long)(1e9 + 1))
#define EP ((long double)(1e-9))
#define MMAX ((long long)(1e9 + 10))
#define HS1 ((long long)(1000001329))
#define HS2 ((long long)(1000001531))
#define MOD ((long long)1000000009ll)
#define SQ 31622780
#define PI 3.14159265358979323846264338327950288419716939937510
#define BG 4294967232ll
#define MH 200008
ll n, m;
string s[2100];

ll b[2100][2100][5];
ll sm[2100][2100][5];
ll f[2100][2100];
void go(ll x, ll y, ll t) {
  if (x < 0 || y < 0 || x >= n || y >= m || s[x][y] == '0' || f[x][y])
    return;
  f[x][y] = 1;
  b[x][y][t]++;
  go(x - 1, y, 3);
  go(x + 1, y, 4);
  go(x, y - 1, 1);
  go(x, y + 1, 2);
}

#ifndef CHKR
int main() {
#else
int doit(fstream &in, fstream &out) {
  cin.rdbuf(in.rdbuf());
  cout.rdbuf(out.rdbuf());
#endif
#ifdef KHOKHO
#ifndef CHKR
  freopen("in.in", "r", stdin);
  freopen("out.out", "w+", stdout);
#endif // CHKR
#endif // KHOKHO
  ll k, p, pas = 0, q;
  cin >> n >> m >> q;
  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] == '1' && !f[i][j]) {
        // cout<<"root "<<i<<" "<<j<<endl;
        go(i, j, 0);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {

      sm[i + 1][j + 1][0] =
          sm[i + 1][j][0] + sm[i][j + 1][0] - sm[i][j][0] + b[i][j][0];

      sm[i + 1][j + 1][1] = sm[i][j + 1][1] + b[i][j][1];
      sm[i + 1][j + 1][2] = sm[i][j + 1][2] + b[i][j][2];

      sm[i + 1][j + 1][3] = sm[i + 1][j][3] + b[i][j][3];
      sm[i + 1][j + 1][4] = sm[i + 1][j][4] + b[i][j][4];

      // cout<<sm[i+1][j+1][1]<<" ";
    }
    // cout<<endl;
  }

  while (q--) {
    ll u, l, d, r;
    cin >> u >> l >> d >> r;
    cout << sm[d][r][0] - sm[d][l - 1][0] - sm[u - 1][r][0] +
                sm[u - 1][l - 1][0] +
                1 * (sm[d][r][3] - sm[d][l - 1][3] + sm[u][r][4] -
                     sm[u][l - 1][4] + sm[d][r][1] - sm[u - 1][r][1] +
                     sm[d][l][2] - sm[u - 1][l][2])
         << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// #define CHKR
#define ll int
#define pb push_back
// #define mp make_pair
#define fr first
#define sc second
#define ARRS int(2e5 + 500)
#define BARRS int(6e4 + 600)
#define MAX ((long long)(1e9 + 1))
#define EP ((long double)(1e-9))
#define MMAX ((long long)(1e9 + 10))
#define HS1 ((long long)(1000001329))
#define HS2 ((long long)(1000001531))
#define MOD ((long long)1000000009ll)
#define SQ 31622780
#define PI 3.14159265358979323846264338327950288419716939937510
#define BG 4294967232ll
#define MH 200008
ll n, m;
string s[2100];

ll b[2100][2100][5];
ll sm[2100][2100][5];
ll f[2100][2100];
void go(ll x, ll y, ll t) {
  if (x < 0 || y < 0 || x >= n || y >= m || s[x][y] == '0' || f[x][y])
    return;
  f[x][y] = 1;
  b[x][y][t]++;
  go(x - 1, y, 3);
  go(x + 1, y, 4);
  go(x, y - 1, 1);
  go(x, y + 1, 2);
}

#ifndef CHKR
int main() {
#else
int doit(fstream &in, fstream &out) {
  cin.rdbuf(in.rdbuf());
  cout.rdbuf(out.rdbuf());
#endif
#ifdef KHOKHO
#ifndef CHKR
  freopen("in.in", "r", stdin);
  freopen("out.out", "w+", stdout);
#endif // CHKR
#endif // KHOKHO
  ll k, p, pas = 0, q;
  cin >> n >> m >> q;
  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] == '1' && !f[i][j]) {
        // cout<<"root "<<i<<" "<<j<<endl;
        go(i, j, 0);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {

      sm[i + 1][j + 1][0] =
          sm[i + 1][j][0] + sm[i][j + 1][0] - sm[i][j][0] + b[i][j][0];

      sm[i + 1][j + 1][1] = sm[i][j + 1][1] + b[i][j][1];
      sm[i + 1][j + 1][2] = sm[i][j + 1][2] + b[i][j][2];

      sm[i + 1][j + 1][3] = sm[i + 1][j][3] + b[i][j][3];
      sm[i + 1][j + 1][4] = sm[i + 1][j][4] + b[i][j][4];

      // cout<<sm[i+1][j+1][1]<<" ";
    }
    // cout<<endl;
  }

  while (q--) {
    ll u, l, d, r;
    cin >> u >> l >> d >> r;
    cout << sm[d][r][0] - sm[d][l - 1][0] - sm[u - 1][r][0] +
                sm[u - 1][l - 1][0] +
                1 * (sm[d][r][3] - sm[d][l - 1][3] + sm[u][r][4] -
                     sm[u][l - 1][4] + sm[d][r][1] - sm[u - 1][r][1] +
                     sm[d][l][2] - sm[u - 1][l][2])
         << endl;
  }

  return 0;
}