Problem p03707 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 1008;

int m, n, q;
int a[N][N], r[N][N], c[N][N];
string s;
int main() {
  cin.sync_with_stdio(0);
  cin >> m >> n >> q;
  for (int i = 1; i <= m; i++) {
    cin >> s;
    for (int j = 1; j <= n; j++)
      a[i][j] = s[j - 1] - '0';
  }
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      if (a[i][j] && a[i - 1][j]) {
        r[i][j]++;
      }
      if (a[i][j] && a[i][j - 1]) {
        c[i][j]++;
      }
    }

  /*for (int i = 1; i <= m; i++)
  {
          for (int j = 1; j <= n; j++)
                  cout << r[i][j] << " ";
          cout << endl;
  }
  cout << endl;*/
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      c[i][j] = c[i][j - 1] + c[i][j];
      r[i][j] = r[i][j - 1] + r[i][j];
      a[i][j] = a[i][j - 1] + a[i][j];
    }
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      c[i][j] = c[i - 1][j] + c[i][j];
      r[i][j] = r[i - 1][j] + r[i][j];
      a[i][j] = a[i - 1][j] + a[i][j];
    }
  /*for (int i = 1; i <= m; i++)
  {
          for (int j = 1; j <= n; j++)
                  cout << r[i][j] << " ";
          cout << endl;
  }
  cout << endl;*/
  for (int i = 0; i < q; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int ans = a[x2][y2] + a[x1 - 1][y1 - 1] - a[x1 - 1][y2] - a[x2][y1 - 1];
    if (y1 < y2)
      ans -= c[x2][y2] + c[x1 - 1][y1] - c[x1 - 1][y2] - c[x2][y1];
    if (x1 < x2)
      ans -= r[x2][y2] + r[x1][y1 - 1] - r[x1][y2] - r[x2][y1 - 1];
    cout << ans << endl;
  }
}

/*
3 4 1
1101
0110
1101
1 1 3 4

 */


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

const int N = 2008;

int m, n, q;
int a[N][N], r[N][N], c[N][N];
string s;
int main() {
  cin.sync_with_stdio(0);
  cin >> m >> n >> q;
  for (int i = 1; i <= m; i++) {
    cin >> s;
    for (int j = 1; j <= n; j++)
      a[i][j] = s[j - 1] - '0';
  }
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      if (a[i][j] && a[i - 1][j]) {
        r[i][j]++;
      }
      if (a[i][j] && a[i][j - 1]) {
        c[i][j]++;
      }
    }

  /*for (int i = 1; i <= m; i++)
  {
          for (int j = 1; j <= n; j++)
                  cout << r[i][j] << " ";
          cout << endl;
  }
  cout << endl;*/
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      c[i][j] = c[i][j - 1] + c[i][j];
      r[i][j] = r[i][j - 1] + r[i][j];
      a[i][j] = a[i][j - 1] + a[i][j];
    }
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      c[i][j] = c[i - 1][j] + c[i][j];
      r[i][j] = r[i - 1][j] + r[i][j];
      a[i][j] = a[i - 1][j] + a[i][j];
    }
  /*for (int i = 1; i <= m; i++)
  {
          for (int j = 1; j <= n; j++)
                  cout << r[i][j] << " ";
          cout << endl;
  }
  cout << endl;*/
  for (int i = 0; i < q; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int ans = a[x2][y2] + a[x1 - 1][y1 - 1] - a[x1 - 1][y2] - a[x2][y1 - 1];
    if (y1 < y2)
      ans -= c[x2][y2] + c[x1 - 1][y1] - c[x1 - 1][y2] - c[x2][y1];
    if (x1 < x2)
      ans -= r[x2][y2] + r[x1][y1 - 1] - r[x1][y2] - r[x2][y1 - 1];
    cout << ans << endl;
  }
}

/*
3 4 1
1101
0110
1101
1 1 3 4

 */