Problem p03707 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int sum[3][1005][1005], lab[3][1005][1005];
int getsum(int x1, int y1, int x2, int y2, int lab) {
  return sum[lab][x2][y2] - sum[lab][x1 - 1][y2] - sum[lab][x2][y1 - 1] +
         sum[lab][x1 - 1][y1 - 1];
}
char str[1000005];
int main() {
  int n, m, q, i, j, a, b, c, d;
  scanf("%d%d%d", &n, &m, &q);
  for (i = 1; i <= n; i++) {
    scanf("%s", str);
    for (j = 0; j < m; j++) {
      if (str[j] == '1')
        lab[0][i][j + 1] = 1;
      else
        lab[0][i][j + 1] = 0;
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      sum[0][i][j] = sum[0][i - 1][j] + sum[0][i][j - 1] + lab[0][i][j] -
                     sum[0][i - 1][j - 1];
    }
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j < m; j++) {
      if (lab[0][i][j] && lab[0][i][j + 1])
        lab[1][i][j] = 1;
      else
        lab[1][i][j] = 0;
    }
  for (i = 1; i <= n; i++) {
    for (j = 1; j < m; j++) {
      sum[1][i][j] = sum[1][i - 1][j] + sum[1][i][j - 1] + lab[1][i][j] -
                     sum[1][i - 1][j - 1];
    }
  }
  for (i = 1; i < n; i++)
    for (j = 1; j <= m; j++) {
      if (lab[0][i][j] && lab[0][i + 1][j])
        lab[2][i][j] = 1;
      else
        lab[2][i][j] = 0;
    }
  for (i = 1; i < n; i++) {
    for (j = 1; j <= m; j++) {
      sum[2][i][j] = sum[2][i - 1][j] + sum[2][i][j - 1] + lab[2][i][j] -
                     sum[2][i - 1][j - 1];
    }
  }
  for (i = 1; i <= q; i++) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    printf("%d\n", getsum(a, b, c, d, 0) - getsum(a, b, c, d - 1, 1) -
                       getsum(a, b, c - 1, d, 2));
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int sum[3][2005][2005], lab[3][2005][2005];
int getsum(int x1, int y1, int x2, int y2, int lab) {
  return sum[lab][x2][y2] - sum[lab][x1 - 1][y2] - sum[lab][x2][y1 - 1] +
         sum[lab][x1 - 1][y1 - 1];
}
char str[1000005];
int main() {
  int n, m, q, i, j, a, b, c, d;
  scanf("%d%d%d", &n, &m, &q);
  for (i = 1; i <= n; i++) {
    scanf("%s", str);
    for (j = 0; j < m; j++) {
      if (str[j] == '1')
        lab[0][i][j + 1] = 1;
      else
        lab[0][i][j + 1] = 0;
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      sum[0][i][j] = sum[0][i - 1][j] + sum[0][i][j - 1] + lab[0][i][j] -
                     sum[0][i - 1][j - 1];
    }
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j < m; j++) {
      if (lab[0][i][j] && lab[0][i][j + 1])
        lab[1][i][j] = 1;
      else
        lab[1][i][j] = 0;
    }
  for (i = 1; i <= n; i++) {
    for (j = 1; j < m; j++) {
      sum[1][i][j] = sum[1][i - 1][j] + sum[1][i][j - 1] + lab[1][i][j] -
                     sum[1][i - 1][j - 1];
    }
  }
  for (i = 1; i < n; i++)
    for (j = 1; j <= m; j++) {
      if (lab[0][i][j] && lab[0][i + 1][j])
        lab[2][i][j] = 1;
      else
        lab[2][i][j] = 0;
    }
  for (i = 1; i < n; i++) {
    for (j = 1; j <= m; j++) {
      sum[2][i][j] = sum[2][i - 1][j] + sum[2][i][j - 1] + lab[2][i][j] -
                     sum[2][i - 1][j - 1];
    }
  }
  for (i = 1; i <= q; i++) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    printf("%d\n", getsum(a, b, c, d, 0) - getsum(a, b, c, d - 1, 1) -
                       getsum(a, b, c - 1, d, 2));
  }
  return 0;
}