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

#include <stdio.h>
int m[5][5][5];
int t[5][5][5];
int b[27];
int c[27];
char str[6];
void CLs() {
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
      for (int k = 0; k < 5; k++)
        t[i][j][k] = 0;
}
int main() {
  int a;
  int T = 0;
  while (scanf("%d", &a), a) {
    if (T)
      printf("\\n");
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) {
        scanf("%s", str);
        for (int k = 0; k < 5; k++)
          m[i][j][k] = str[k] - '0';
      }
    int d, e;
    scanf("%d", &d);
    for (int i = 0; i < d; i++)
      scanf("%d", b + i);
    scanf("%d", &e);
    for (int i = 0; i < e; i++)
      scanf("%d", c + i);
    for (int i = 0; i < a; i++) {
      CLs();
      for (int j = 0; j < 5; j++)
        for (int k = 0; k < 5; k++)
          for (int l = 0; l < 5; l++)
            for (int x = -1; x <= 1; x++)
              for (int y = -1; y <= 1; y++)
                for (int z = -1; z <= 1; z++) {
                  if (x == 0 && y == 0 && z == 0)
                    continue;
                  if (!m[j][k][l] || j + x < 0 || j + x > 4 || k + y < 0 ||
                      k + y > 4 || l + z < 0 || l + z > 4)
                    continue;
                  t[j + x][k + y][l + z]++;
                }
      for (int j = 0; j < 5; j++)
        for (int k = 0; k < 5; k++)
          for (int l = 0; l < 5; l++)
            if (m[j][k][l]) {
              bool ok = false;
              for (int p = 0; p < e; p++)
                if (t[j][k][l] == c[p])
                  ok = true;
              if (!ok)
                m[j][k][l] = 0;
            } else {
              bool ok = false;
              for (int p = 0; p < d; p++)
                if (t[j][k][l] == b[p])
                  ok = true;
              if (ok)
                m[j][k][l] = 1;
            }
    }
    printf("Case %d:\\n", ++T);
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        for (int k = 0; k < 5; k++)
          printf("%d", m[i][j][k]);
        printf("\\n");
      }
      if (i < 4)
        printf("\\n");
    }
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int m[5][5][5];
int t[5][5][5];
int b[27];
int c[27];
char str[6];
inline void CLS() {
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
      for (int k = 0; k < 5; k++)
        t[i][j][k] = 0;
}
int main() {
  int a;
  int T = 0;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++) {
        scanf("%s", str);
        for (int k = 0; k < 5; k++)
          m[i][j][k] = str[k] - '0';
      }
    int d, e;
    scanf("%d", &d);
    for (int i = 0; i < d; i++)
      scanf("%d", b + i);
    scanf("%d", &e);
    for (int i = 0; i < e; i++)
      scanf("%d", c + i);
    for (int i = 0; i < a; i++) {
      CLS();
      for (int j = 0; j < 5; j++)
        for (int k = 0; k < 5; k++)
          for (int l = 0; l < 5; l++)
            for (int x = -1; x <= 1; x++)
              for (int y = -1; y <= 1; y++)
                for (int z = -1; z <= 1; z++) {
                  if (x == 0 && y == 0 && z == 0)
                    continue;
                  if (!m[i][j][k] || j + x < 0 || j + x > 4 || k + y < 0 ||
                      k + y > 4 || l + z < 0 || l + z > 4)
                    continue;
                  t[j + x][k + y][l + z]++;
                }
      for (int j = 0; j < 5; j++)
        for (int k = 0; k < 5; k++)
          for (int l = 0; l < 5; l++)
            if (m[i][j][k]) {
              bool ok = false;
              for (int p = 0; p < e; p++)
                if (t[i][j][k] == c[p])
                  ok = true;
              if (!ok)
                m[i][j][k] = 0;
            } else {
              bool ok = false;
              for (int p = 0; p < d; p++)
                if (t[i][j][k] == b[p])
                  ok = true;
              if (ok)
                m[i][j][k] = 1;
            }
    }
    printf("Case %d:\\n", ++T);
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        for (int k = 0; k < 5; k++)
          printf("%d", m[i][j][k]);
        printf("\\n");
      }
      printf("\\n");
    }
  }
}