Problem p01000 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int z[1000][7][7][7], n, m, p;
int a[27], b[27];
int getsum(int t, int i, int j, int k) {
  int S = 0;
  for (int I = -1; I <= 1; I++) {
    for (int J = -1; J <= 1; J++) {
      for (int K = -1; K <= 1; K++) {
        S += z[t][i + I][j + J][k + K];
      }
    }
  }
  return S - z[t][i][j][k];
}
int main() {
  int cnt = 0;
  while (true) {
    for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 7; j++) {
        for (int k = 0; k < 7; k++) {
          for (int l = 0; l < 7; l++) {
            z[i][j][k][l] = 0;
          }
        }
      }
    }
    for (int i = 0; i < 27; i++) {
      a[i] = 0;
      b[i] = 0;
    }
    cin >> n;
    cnt++;
    if (n == 0)
      break;
    if (cnt >= 2)
      cout << endl;
    for (int i = 1; i <= 5; i++) {
      for (int j = 1; j <= 5; j++) {
        for (int k = 1; k <= 5; k++) {
          char c;
          cin >> c;
          if (c == '0')
            z[0][i][j][k] = 0;
          if (c == '1')
            z[0][i][j][k] = 1;
        }
      }
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> p;
      a[p] = 1;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> p;
      b[p] = 1;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= 5; j++) {
        for (int k = 1; k <= 5; k++) {
          for (int l = 1; l <= 5; l++) {
            int G = getsum(i, j, k, l);
            if (z[i][j][k][l] == 0 && a[G] == 1) {
              z[i + 1][j][k][l] = 1;
            } else if (z[i][j][k][l] == 1 && b[G] == 0) {
              z[i + 1][j][k][l] = 0;
            } else {
              z[i + 1][j][k][l] = z[i][j][k][l];
            }
          }
        }
      }
    }
    cout << "Case " << cnt << ':' << endl;
    for (int i = 1; i <= 5; i++) {
      for (int j = 1; j <= 5; j++) {
        for (int k = 1; k <= 5; k++) {
          cout << z[n][i][j][k];
        }
        cout << endl;
      }
      if (i <= 4) {
        cout << endl;
      }
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int z[1000][7][7][7], n, m, p;
int a[10], b[10];
int getsum(int t, int i, int j, int k) {
  int S = 0;
  for (int I = -1; I <= 1; I++) {
    for (int J = -1; J <= 1; J++) {
      for (int K = -1; K <= 1; K++) {
        S += z[t][i + I][j + J][k + K];
      }
    }
  }
  return S;
}
int main() {
  int cnt = 0;
  while (true) {
    for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 7; j++) {
        for (int k = 0; k < 7; k++) {
          for (int l = 0; l < 7; l++) {
            z[i][j][k][l] = 0;
          }
        }
      }
    }
    for (int i = 0; i < 10; i++) {
      a[i] = 0;
      b[i] = 0;
    }
    cin >> n;
    cnt++;
    if (n == 0)
      break;
    for (int i = 1; i <= 5; i++) {
      for (int j = 1; j <= 5; j++) {
        for (int k = 1; k <= 5; k++) {
          char c;
          cin >> c;
          if (c == '0')
            z[0][i][j][k] = 0;
          if (c == '1')
            z[0][i][j][k] = 1;
        }
      }
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> p;
      a[p] = 1;
    }
    for (int i = 0; i < m; i++) {
      cin >> p;
      b[p] = 1;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= 5; j++) {
        for (int k = 1; k <= 5; k++) {
          for (int l = 1; l <= 5; l++) {
            int G = getsum(i, j, k, l);
            if (z[i][j][k][l] == 0 && a[G] == 1) {
              z[i + 1][j][k][l] = 1;
            } else if (z[i][j][k][l] == 1 && b[G] == 1) {
              z[i + 1][j][k][l] = 0;
            } else {
              z[i + 1][j][k][l] = z[i][j][k][l];
            }
          }
        }
      }
    }
    cout << "Case " << cnt << ':' << endl;
    for (int i = 1; i <= 5; i++) {
      for (int j = 1; j <= 5; j++) {
        for (int k = 1; k <= 5; k++) {
          cout << z[n][i][j][k];
        }
        cout << endl;
      }
      cout << endl;
    }
  }
  return 0;
}