Problem p03707 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
//----------------------------------------------------------
// Macro
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define sqr(x) ((x) * (x))
#define pb push_back
#define clr clear()
#define inf (1 << 30)
#define ins insert
#define maxn 1e6
//----------------------------------------------------------
// TypeDef
typedef long long ll;
typedef unsigned long long ull;
typedef string st;
typedef vector<int> vi;
//----------------------------------------------------------
// Variables
int n, m, q;
bool a[1001][1001] = {};
int d[1001][1001] = {}, r[1001][1001] = {}, v[1001][1001] = {};
//----------------------------------------------------------
void Input() {
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      a[i][j] = (c - '0');
      d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1];
      r[i][j] = r[i - 1][j] + r[i][j - 1] - r[i - 1][j - 1];
      v[i][j] = v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1];
      if (a[i][j] and a[i - 1][j])
        d[i][j]++;
      if (a[i][j] and a[i][j - 1])
        r[i][j]++;
      if (a[i][j])
        v[i][j]++;
    }
}
void Solve() {
  while (q--) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int a = r[x2][y2] - r[x1 - 1][y2] - r[x2][y1] + r[x1 - 1][y1],
        b = d[x2][y2] - d[x1][y2] - d[x2][y1 - 1] + d[x1][y1 - 1],
        c = v[x2][y2] - v[x1 - 1][y2] - v[x2][y1 - 1] + v[x1 - 1][y1 - 1];
    // cout << a << ' ' << b << ' ' << c << '\n';
    cout << c - a - b << '\n';
  }
}
//----------------------------------------------------------
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  Input();
  Solve();
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
//----------------------------------------------------------
// Macro
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define sqr(x) ((x) * (x))
#define pb push_back
#define clr clear()
#define inf (1 << 30)
#define ins insert
#define maxn 1e6
//----------------------------------------------------------
// TypeDef
typedef long long ll;
typedef unsigned long long ull;
typedef string st;
typedef vector<int> vi;
//----------------------------------------------------------
// Variables
int n, m, q;
bool a[2002][2002] = {};
int d[2002][2002] = {}, r[2002][2002] = {}, v[2002][2002] = {};
//----------------------------------------------------------
void Input() {
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      char c;
      cin >> c;
      a[i][j] = (c - '0');
      d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1];
      r[i][j] = r[i - 1][j] + r[i][j - 1] - r[i - 1][j - 1];
      v[i][j] = v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1];
      if (a[i][j] and a[i - 1][j])
        d[i][j]++;
      if (a[i][j] and a[i][j - 1])
        r[i][j]++;
      if (a[i][j])
        v[i][j]++;
    }
}
void Solve() {
  while (q--) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int a = r[x2][y2] - r[x1 - 1][y2] - r[x2][y1] + r[x1 - 1][y1],
        b = d[x2][y2] - d[x1][y2] - d[x2][y1 - 1] + d[x1][y1 - 1],
        c = v[x2][y2] - v[x1 - 1][y2] - v[x2][y1 - 1] + v[x1 - 1][y1 - 1];
    // cout << a << ' ' << b << ' ' << c << '\n';
    cout << c - a - b << '\n';
  }
}
//----------------------------------------------------------
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  Input();
  Solve();
}