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

#include <algorithm>
#include <climits>
#include <iostream>

using namespace std;

int dp[2][1 << 22];

int main(void) {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (!n && !m)
      break;

    int b[505][505] = {0};
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < m; j++) {
        char c;
        cin >> c;
        b[i][j] = c - '0';
      }
    }

    int ans = 0;
    if (n <= 22) {
      int S = 1 << n;
      for (int i = 0; i < S; i++) {
        int cnt = 0;
        int ingredient[501] = {0}; // 材料
        for (int j = 1; j <= n; j++) {
          if ((i & (1 << (j - 1))) == 0)
            continue;
          cnt++;
          for (int k = 0; k < m; k++)
            ingredient[k] ^= b[j][k];
        }
        bool flag = true;
        for (int j = 0; j < m; j++) {
          if (ingredient[j] == 1) {
            flag = false;
            break;
          }
        }
        if (flag)
          ans = max(ans, cnt);
      }
    } else {
      int B[501] = {0};
      for (int i = 1; i <= n; i++) {
        for (int j = 0; j < m; j++) {
          B[i] |= (b[i][j] << j);
        }
      }

      int S = 1 << m;
      for (int i = 0; i < S; i++)
        dp[0][i] = dp[1][i] = INT_MIN;
      dp[0][0] = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < S; j++) {
          dp[(i + 1) % 2][j ^ B[i + 1]] =
              max(dp[(i + 1) % 2][j ^ B[i + 1]], dp[i % 2][j] + 1);
          dp[(i + 1) % 2][j] = max(dp[(i + 1) % 2][j], dp[i % 2][j]);
        }
      }
      ans = dp[n % 2][0];
    }
    cout << ans << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <limits.h>
#include <stdio.h>
#include <string.h>

int b[501][501];
int B[501];
int sum[501];
int dp[30][1 << 22];

int main(void) {
  int n, m, ans, count;
  bool flag = true;

  while (1) {
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;

    ////////////////////////////////////////////////////////
    if (n < 22) {
      /*input*/
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          scanf(" %c", &b[i][j]);
          b[i][j] -= '0';
        }
      }

      ans = 0;
      for (int i = 0; i < (1 << n); i++) {
        count = 0;
        memset(sum, 0, sizeof(sum));
        for (int j = 0; j < n; j++) {
          if ((i >> j) % 2 == 0) {
            count++;
            for (int k = 0; k < m; k++)
              sum[k] += b[j][k];
          }
        }

        flag = true;
        for (int k = 0; k < m; k++) {
          if (sum[k] % 2 == 1) {
            flag = false;
            break;
          }
        }
        if (flag == true && ans < count)
          ans = count;
      }
      printf("%d\\n", ans);
      /////////////////////////////////////////////////////////////
    } else {
      /*input*/
      for (int i = 0; i < n; i++) {
        B[i] = 0;
        for (int j = 0; j < m; j++) {
          scanf(" %c", &b[i][j]);
          b[i][j] = b[i][j] - '0';
          B[i] += (b[i][j] << j);
        }
      }

      for (int i = 0; i < 30; i++) {
        for (int j = 0; j < (1 << m); j++) {
          dp[i][j] = INT_MIN;
        }
      }

      dp[0][0] = 0;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < (1 << m); j++) {
          if (dp[i][j] < 0)
            continue;
          if (dp[i + 1][j] < dp[i][j])
            dp[i + 1][j] = dp[i][j];
          if (dp[i + 1][j ^ B[i]] < dp[i][j] + 1)
            dp[i + 1][j ^ B[i]] = dp[i][j] + 1;
        }
      }
      ans = dp[n][0];
      printf("%d\\n", ans);
    }
    ////////////////////////////////////////////////////////////////
  } // endwhile
  return 0;
}