Problem p02892 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pii pair<int, int>
#define pld pair<ld, ld>
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define MP make_pair
#define mset(x) memset(x, 0, sizeof(x));
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define inf 1000000000000007
const int N = 206;
ll n, dp[N][N], col[N];
string a[N];
void run() {
  ll i, j, k, ans = 0;
  queue<ll> q;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] = '0' + a[i];
    for (j = 1; j <= n; j++)
      if (a[i][j] == '1')
        dp[i][j] = 1;
      else if (i == j)
        dp[i][i] = 0;
      else
        dp[i][j] = 5000;
  }
  q.push(1);
  col[1] = 1;
  while (!q.empty()) {
    i = q.front();
    q.pop();
    for (j = 1; j <= n; j++) {
      if (a[i][j] == '1') {
        if (col[j] == 0) {
          col[j] = -col[i];
          q.push(j);
        } else if (col[j] == col[i]) {
          cout << -1;
          return;
        }
      }
    }
  }
  cout << 1 / 0;
  // for(k=1;k<=n;k++)
  // {
  //     for(i=1;i<=n;i++)
  //     {
  //         for(j=1;j<=n;j++)
  //         {
  //             dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
  //             if(dp[i][j]<5000) ans=max(ans,dp[i][j]);
  //         }
  //     }
  // }
  // cout<<ans+1;
}

void setup() {
  ios;
  // cout << setprecision(20);
  //  #ifndef ONLINE_JUDGE
  //  freopen("input", "r", stdin);
  //  freopen("output", "w", stdout);
  //  #endif
}

signed main() {
  setup();
  int tests = 1;
  // cin >> tests;
  for (int i = 1; i <= tests; i++)
    run();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define pii pair<int, int>
#define pld pair<ld, ld>
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define MP make_pair
#define mset(x) memset(x, 0, sizeof(x));
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define inf 1000000000000007
const int N = 206;
ll n, dp[N][N], col[N];
string a[N];
void run() {
  ll i, j, k, ans = 0;
  queue<ll> q;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] = '0' + a[i];
    for (j = 1; j <= n; j++)
      if (a[i][j] == '1')
        dp[i][j] = 1;
      else if (i == j)
        dp[i][i] = 0;
      else
        dp[i][j] = 5000;
  }
  q.push(1);
  col[1] = 1;
  while (!q.empty()) {
    i = q.front();
    q.pop();
    for (j = 1; j <= n; j++) {
      if (a[i][j] == '1') {
        if (col[j] == 0) {
          col[j] = -col[i];
          q.push(j);
        } else if (col[j] == col[i]) {
          cout << -1;
          return;
        }
      }
    }
  }

  for (k = 1; k <= n; k++) {
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
      }
    }
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      if (dp[i][j] < 5000)
        ans = max(ans, dp[i][j]);
  cout << ans + 1;
}

void setup() {
  ios;
  // cout << setprecision(20);
  //  #ifndef ONLINE_JUDGE
  //  freopen("input", "r", stdin);
  //  freopen("output", "w", stdout);
  //  #endif
}

signed main() {
  setup();
  int tests = 1;
  // cin >> tests;
  for (int i = 1; i <= tests; i++)
    run();
  return 0;
}