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

#include "bits/stdc++.h"
#include <string>

using namespace std;

int a, b, k[1005][1005], c, l[5], m[505][505], ans = 0;

int main() {
  cin >> a >> b;
  for (int i = 0; i < b; ++i) {
    char str[1234];
    scanf("%s", str);
    for (int j = 0; j < b; ++j) {
      if (str[j] == '1')
        k[i][j] = 1;
      if (str[j] == '0')
        k[i][j] = 0;
    }
  }

  for (int i = 0; i < b / 2; ++i) {
    for (int j = 0; j < b / 2; ++j) {
      int count = 0;
      if (k[i][j] == 1)
        count++;
      if (k[i][b - j - 1] == 1)
        count++;
      if (k[b - i - 1][b - j - 1] == 1)
        count++;
      if (k[b - i - 1][j] == 1)
        count++;
      m[i][j] = count;
    }
  }
  bool flag = true;
  for (int i = 0; i < b / 2; ++i) {
    for (int j = 0; j < b / 2; ++j) {
      if (m[i][j] != 0 && m[i][j] != 4) {
        flag = false;
        goto syamu;
      }
    }
  }
syamu:
  if (flag)
    ans++;

  while (max(0, a - 1)) {
    flag = true;
    cin >> c;
    for (int i = 0; i < c; ++i) {
      cin >> l[0] >> l[1];
      l[0]--;
      l[1]--;
      if (k[l[0]][l[1]] == 1) {
        k[l[0]][l[1]] = 0;
        m[min(l[0], b - l[0] - 1)][min(l[1], b - l[1] - 1)]--;
      } else if (k[l[0]][l[1]] == 0) {
        k[l[0]][l[1]] = 1;
        m[min(l[0], b - l[0] - 1)][min(l[1], b - l[1] - 1)]++;
      }
    }
    for (int i = 0; i < (b / 2); ++i) {
      for (int j = 0; j < (b / 2); ++j) {
        if (m[i][j] != 0 && m[i][j] != 4) {
          flag = false;
          goto aa;
        }
      }
    }
  aa:
    if (flag)
      ans++;
    a--;
  }
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include "bits/stdc++.h"
#include <string>

using namespace std;

int a, b, k[1005][1005], c, l[5], m[505][505], ans = 0;

int main() {
  cin >> a >> b;
  char buf;
  scanf("%c", &buf);
  scanf("%c", &buf);
  for (int i = 0; i < b; ++i) {
    for (int j = 0; j < b; ++j) {
      scanf("%c", &buf);
      if (buf == '1')
        k[i][j] = 1;
      if (buf == '0')
        k[i][j] = 0;
    }
    scanf("%c", &buf);
  }

  for (int i = 0; i < b / 2; ++i) {
    for (int j = 0; j < b / 2; ++j) {
      int count = 0;
      if (k[i][j] == 1)
        count++;
      if (k[i][b - j - 1] == 1)
        count++;
      if (k[b - i - 1][b - j - 1] == 1)
        count++;
      if (k[b - i - 1][j] == 1)
        count++;
      m[i][j] = count;
    }
  }
  bool flag = true;
  for (int i = 0; i < b / 2; ++i) {
    for (int j = 0; j < b / 2; ++j) {
      if (m[i][j] != 0 && m[i][j] != 4) {
        flag = false;
        goto syamu;
      }
    }
  }
syamu:
  if (flag)
    ans++;

  while (max(0, a - 1)) {
    flag = true;
    cin >> c;
    for (int i = 0; i < c; ++i) {
      cin >> l[0] >> l[1];
      l[0]--;
      l[1]--;
      if (k[l[0]][l[1]] == 1) {
        k[l[0]][l[1]] = 0;
        m[min(l[0], b - l[0] - 1)][min(l[1], b - l[1] - 1)]--;
      } else if (k[l[0]][l[1]] == 0) {
        k[l[0]][l[1]] = 1;
        m[min(l[0], b - l[0] - 1)][min(l[1], b - l[1] - 1)]++;
      }
    }
    for (int i = 0; i < (b / 2); ++i) {
      for (int j = 0; j < (b / 2); ++j) {
        if (m[i][j] != 0 && m[i][j] != 4) {
          flag = false;
          goto aa;
        }
      }
    }
  aa:
    if (flag)
      ans++;
    a--;
  }
  cout << ans << endl;
}