Problem p03707 (C++)

Buggy submission (status: Runtime Error):
/// not today

#include <bits/stdc++.h>
using namespace std;

#define SZ(x) (int)(x).size()
#define pb push_back
#define ll long long
#define xmax(x, y) (x) = max((x), (y))
#define xmin(x, y) (x) = min((x), (y))
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<long long, long long>
#define int long long
#define pipii pair<int, pair<int, int>>
#define pf push_front

const int maxn = 805;
bool a[maxn][maxn];
int h[maxn][maxn], v[maxn][maxn], nod[maxn][maxn];

int32_t main() {
  int n, m, Q;
  cin >> n >> m >> Q;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      a[i][j] = s[j] - '0';
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      nod[i][j] = nod[i + 1][j] + nod[i][j + 1] - nod[i + 1][j + 1] + a[i][j];
      v[i][j] = v[i + 1][j] + v[i][j + 1] - v[i + 1][j + 1] +
                (a[i][j] and a[i][j + 1]);
      h[i][j] = h[i + 1][j] + h[i][j + 1] - h[i + 1][j + 1] +
                (a[i][j] and a[i + 1][j]);
/*            cout <<i <<' ' <<j <<endl;
            cout <<"nod" <<' ' <<nod[i][j] <<endl;
            cout <<'v' <<' ' <<v[i][j] <<endl;
            cout <<'h' <<' ' <<h[i][j] <<endl;
*/        }
  }
  while (Q--) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
int eg = h[x1][y1] - h[x2][y1] - h[x1][y2 + 1] + h[x2][y2 + 1] + v[x1][y1] -
         v[x1][y2] - v[x2 + 1][y1] + v[x2 + 1][y2];
cout << (nod[x1][y1] - nod[x1][y2 + 1] - nod[x2 + 1][y1] +
         nod[x2 + 1][y2 + 1]) -
            eg
     << '\n';
  }
}


Fixed submission:
/// not today

#include <bits/stdc++.h>
using namespace std;

#define SZ(x) (int)(x).size()
#define pb push_back
#define ll long long
#define xmax(x, y) (x) = max((x), (y))
#define xmin(x, y) (x) = min((x), (y))
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<long long, long long>
#define int long long
#define pipii pair<int, pair<int, int>>
#define pf push_front

const int maxn = 2005;
bool a[maxn][maxn];
int h[maxn][maxn], v[maxn][maxn], nod[maxn][maxn];

int32_t main() {
  int n, m, Q;
  cin >> n >> m >> Q;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      a[i][j] = s[j] - '0';
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      nod[i][j] = nod[i + 1][j] + nod[i][j + 1] - nod[i + 1][j + 1] + a[i][j];
      v[i][j] = v[i + 1][j] + v[i][j + 1] - v[i + 1][j + 1] +
                (a[i][j] and a[i][j + 1]);
      h[i][j] = h[i + 1][j] + h[i][j + 1] - h[i + 1][j + 1] +
                (a[i][j] and a[i + 1][j]);
/*            cout <<i <<' ' <<j <<endl;
            cout <<"nod" <<' ' <<nod[i][j] <<endl;
            cout <<'v' <<' ' <<v[i][j] <<endl;
            cout <<'h' <<' ' <<h[i][j] <<endl;
*/        }
  }
  while (Q--) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--;
y1--;
x2--;
y2--;
int eg = h[x1][y1] - h[x2][y1] - h[x1][y2 + 1] + h[x2][y2 + 1] + v[x1][y1] -
         v[x1][y2] - v[x2 + 1][y1] + v[x2 + 1][y2];
cout << (nod[x1][y1] - nod[x1][y2 + 1] - nod[x2 + 1][y1] +
         nod[x2 + 1][y2 + 1]) -
            eg
     << '\n';
  }
}