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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double eps = 10e-6;

int diagLLUR(int n, int **mat) {
  int res = 0;
  for (int y = 0; y < n; y++) {
    int c = 0;
    for (int x = 0, dy = y; x < n && dy >= 0; x++, dy--) {
      if (mat[dy][x] == 0) {
        res = max(res, c);
        c = 0;
      } else
        c++;
    }
    res = max(c, res);
  }

  for (int x = 0; x < n; x++) {
    int c = 0;
    for (int dx = x, y = n - 1; dx < n && y >= 0; dx++, y--) {
      if (mat[y][dx] == 0) {
        res = max(res, c);
        c = 0;
      } else
        c++;
    }
    res = max(c, res);
  }

  return res;
}

int diagULLR(int n, int **mat) {
  int res = 0;
  for (int x = n - 1; x >= 0; x--) {
    int c = 0;
    for (int dx = x, y = 0; dx < n && y < n; dx++, y++) {
      if (mat[y][dx] == 0) {
        res = max(res, c);
        c = 0;
      } else
        c++;
    }
    res = max(c, res);
  }

  for (int y = 0; y < n; y++) {
    int c = 0;
    for (int x = 0, dy = y; x < n && dy < n; x++, dy++) {
      if (mat[dy][x] == 0) {
        res = max(res, c);
        c = 0;
      } else
        c++;
    }
    res = max(c, res);
  }

  return res;
}

int vert(int n, int **mat) {
  int res = 0;
  for (int x = 0; x < n; x++) {
    int c = 0;
    for (int y = 0; y < n; y++) {
      if (mat[y][x] == 0) {
        res = max(res, c);
        c = 0;
      } else
        c++;
    }

    res = max(c, res);
  }

  return res;
}

int hori(int n, int **mat) {
  int res = 0;
  for (int y = 0; y < n; y++) {
    int c = 0;
    for (int x = 0; x < n; x++) {
      if (mat[y][x] == 0) {
        res = max(res, c);
        c = 0;
      } else
        c++;
    }

    res = max(c, res);
  }

  return res;
}

int main() {

  int n;

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

    int **mat = new int *[n];
    for (int i = 0; i < n; i++)
      mat[i] = new int[n]();

    for (int y = 0; y < n; y++) {
      char buf[512];
      scanf("%s", buf);
      for (int x = 0; x < n; x++) {
        mat[y][x] = buf[x] - '0';
      }
    }

    printf("%d\\n", max(max(diagLLUR(n, mat), hori(n, mat)),
                       max(diagULLR(n, mat), vert(n, mat))));

    for (int i = 0; i < n; i++)
      delete[] mat[i];
    delete[] mat;
  }
}

Error: WA

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

static const double eps = 10e-6;

int mat[255][255];

int diagLLUR(int n, int **mat) {
  int res = 0;
  for (int y = 0; y < n; y++) {
    int isok = 1;
    for (int x = 0, dy = y; x < n, dy >= 0; x++, dy--) {
      if (mat[dy][x] == 0)
        isok = 0;
    }
    res = max((y + 1) * isok, res);
  }

  for (int x = 0; x < n; x++) {
    int isok = 1;
    for (int dx = x, y = n - 1; dx < n, y >= 0; dx++, y--) {
      if (mat[y][dx] == 0)
        isok = 0;
    }
    res = max((n - x) * isok, res);
  }

  return res;
}

int diagULLR(int n, int **mat) {
  int res = 0;
  for (int x = n - 1; x >= 0; x--) {
    int isok = 1;
    for (int dx = x, y = 0; dx < n, y < n; dx++, y++) {
      if (mat[y][dx] == 0)
        isok = 0;
    }
    res = max((n - x) * isok, res);
  }

  for (int y = 0; y < n; y++) {
    int isok = 1;
    for (int x = 0, dy = y; x < n, dy < n; x++, dy++) {
      if (mat[dy][x] == 0)
        isok = 0;
    }
    res = max((n - y) * isok, res);
  }

  return res;
}

int vert(int n, int **mat) {
  int res = 0;
  for (int x = 0; x < n; x++) {
    int isok = 1;
    for (int y = 0; y < n; y++) {
      if (mat[y][x] == 0)
        isok = 0;
    }

    res = max(n * isok, res);
  }

  return res;
}

int hori(int n, int **mat) {
  int res = 0;
  for (int y = 0; y < n; y++) {
    int isok = 1;
    for (int x = 0; x < n; x++) {
      if (mat[y][x] == 0)
        isok = 0;
    }

    res = max(n * isok, res);
  }

  return res;
}

int main() {

  int n;

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

    int **mat = new int *[n];
    for (int i = 0; i < n; i++)
      mat[i] = new int[n]();

    for (int y = 0; y < n; y++) {
      char buf[128];
      scanf("%s", buf);
      for (int x = 0; x < n; x++) {
        mat[y][x] = buf[x] - '0';
      }
    }
    printf("%d\\n", max(max(diagLLUR(n, mat), hori(n, mat)),
                       max(diagULLR(n, mat), vert(n, mat))));

    for (int i = 0; i < n; i++)
      delete[] mat[i];
    delete[] mat;
  }
}