Problem p02892 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#pragma GCC optimize("Ofast")
using namespace std;
const long long INF = 20000000000;
// setprecision(2) <<

const int MOD = 1000000007;

long long max(long long a, long long b) {
  if (a < b)
    return b;
  else
    return a;
}
long long min(long long a, long long b) {
  if (a > b)
    return b;
  else
    return a;
}
long long gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(a % b, b);
}

long long lcm(long long a, long long b) { return a * b / gcd(a, b); }

long long com(long long n, long long k) {}
long long getDigit(long long n) {
  if (n == 1)
    return 1;
  else
    return log10(n) + 1;
}
class G {
  long long node;
  long long w;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long N;
  cin >> N;
  string Gs[N + 1];
  int G[N][N];
  for (int i = 0; i < N; i++) {
    cin >> Gs[i];
    for (int j = 0; j < N; j++) {
      G[i][j] = (int)(Gs[i][j] - '0');
    }
  }
  // cout<<G[0][1];
  // 2graph or not
  stack<long long> que;
  vector<int> color(N + 2, -1);
  vector<bool> ex(N + 2, false);
  // 0 not 1 black 2 white
  color[0] = 1;
  que.push(0);
  while (!que.empty()) {
    int u = que.top();
    que.pop();
    ex[u] = true;
    // cout<<u<<endl;
    for (int i = 0; i < N; i++) {
      if (G[i][u] == 1) {
        if (ex[i] == false)
          que.push(i);
        if (color[i] == -1) {
          color[i] = !color[u];
          continue;
        }
        if (color[i] == color[u]) {
          cout << -1 << endl;
          return 0;
        }
      }
    }
  }
  return -1;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#pragma GCC optimize("Ofast")
using namespace std;
const long long INF = 20000000000;
// setprecision(2) <<

const int MOD = 1000000007;

long long max(long long a, long long b) {
  if (a < b)
    return b;
  else
    return a;
}
long long min(long long a, long long b) {
  if (a > b)
    return b;
  else
    return a;
}
long long gcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    return gcd(a % b, b);
}

long long lcm(long long a, long long b) { return a * b / gcd(a, b); }

long long com(long long n, long long k) {}
long long getDigit(long long n) {
  if (n == 1)
    return 1;
  else
    return log10(n) + 1;
}
class G {
  long long node;
  long long w;
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long N;
  cin >> N;
  string Gs[N + 1];
  int G[N][N];
  for (int i = 0; i < N; i++) {
    cin >> Gs[i];
    for (int j = 0; j < N; j++) {
      G[i][j] = (int)(Gs[i][j] - '0');
    }
  }
  // cout<<G[0][1];
  // 2graph or not
  stack<long long> que;
  vector<int> color(N + 2, -1);
  vector<bool> ex(N + 2, false);
  // 0 not 1 black 2 white
  color[0] = 1;
  que.push(0);
  while (!que.empty()) {
    int u = que.top();
    que.pop();
    ex[u] = true;
    // cout<<u<<endl;
    for (int i = 0; i < N; i++) {
      if (G[i][u] == 1) {
        if (ex[i] == false)
          que.push(i);
        if (color[i] == -1) {
          color[i] = !color[u];
          continue;
        }
        if (color[i] == color[u]) {
          cout << -1 << endl;
          return 0;
        }
      }
    }
  }

  //
  long long d[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = G[i][j];
      if (G[i][j] != 1) {
        d[i][j] = INF;
      }
      if (i == j)
        d[i][j] = 0;
    }
  }

  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      // cout<<d[i][j];
      ans = max(ans, d[i][j]);
    }
  }
  cout << ans + 1 << endl;
}