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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n;
char grid[256][256];

int solve() {
  int res = 0, c;
  rep(i, n) {
    c = 0;
    rep(j, n) {
      if (grid[i][j] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  rep(j, n) {
    c = 0;
    rep(i, n) {
      if (grid[i][j] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  rep(i, n) {
    c = 0;
    rep(j, n) {
      if (i - j < 0)
        break;
      if (grid[j][i - j] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  rep(i, n) {
    c = 0;
    rep(j, n) {
      if (n - j < 0 || n - i + j - 1 >= n || n - i + j - 1 < 0)
        break;
      if (grid[n - i + j - 1][n - j] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  rep(i, n) {
    c = 0;
    rep(j, n) {
      if (n - i + j - 1 >= n)
        break;
      if (grid[j][n - i + j - 1] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  rep(i, n) {
    c = 0;
    rep(j, n) {
      if (i + j >= n)
        break;
      if (grid[i + j][j] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  return res;
}

int main() {
  while (scanf("%d", &n) && n) {
    memset(grid, 0, sizeof(grid));
    rep(i, n) scanf("%s", grid[i]);
    printf("%d\\n", solve());
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define F first
#define S second
#define INF 1 << 30

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int n;
char grid[256][256];

int solve() {
  int res = 0, c;
  rep(i, n) {
    c = 0;
    rep(j, n) {
      if (grid[i][j] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  rep(j, n) {
    c = 0;
    rep(i, n) {
      if (grid[i][j] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  rep(i, n) {
    c = 0;
    rep(j, n) {
      if (i - j < 0)
        break;
      if (grid[j][i - j] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  rep(i, n) {
    c = 0;
    rep(j, n) {
      if (n - j < 0)
        break;
      if (grid[n - i + j - 1][n - j] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  rep(i, n) {
    c = 0;
    rep(j, n) {
      if (n - i + j - 1 >= n)
        break;
      if (grid[j][n - i + j - 1] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  rep(i, n) {
    c = 0;
    rep(j, n) {
      if (i + j >= n)
        break;
      if (grid[i + j][j] == '1')
        res = max(res, ++c);
      else
        c = 0;
    }
  }

  return res;
}

int main() {
  while (scanf("%d", &n) && n) {
    memset(grid, 0, sizeof(grid));
    rep(i, n) scanf("%s", grid[i]);
    printf("%d\\n", solve());
  }
  return 0;
}