Language: C++

#include<bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;

#define PI (2*acos(0.0))
#define eps 1e-9
#define pcase cout<<"Case "<<++cs<<": "
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define mp make_pair
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define fail {cout << -1 << endl; return 0;}

int main()
{
    int i = 0, j = 0, cs = 0;
    int n; cin>>n;
    vi v[n+5];
    for(i = 1; i <= n; i++){
        for(j = 1;j  <= n; j++){
            int in;
            scanf("%1d", &in);
            if(in) v[i].pb(j);
        }
    }
//    for(i = 1; i <= n; i++){
//        for(j = 0; j < v[i].size(); j++) cout << v[i][j] << " ";
//        cout <<endl;
//    }
    bool visited[n+5]; memset(visited, false, sizeof(visited));
    queue<pair<int,int>> q;
    q.push({1,n+2});
    visited[1] = true;
    deque<vi> ans(2*n+10);
    ans[n+2].pb(1);
    bool flag = 0;
    while(!q.empty()){
        queue<pair<int,int>> para = q;
//        while(!para.empty()){
//            ii l = para.front(); para.pop();
//            cout << l.ff << " ";
//        }
//        cout << endl;
        ii p = q.front();
        int node = p.ff;
        int pos = p.ss;
//        watch(node);
//        watch(pos);
        q.pop();
        for(i = 0;  i < v[node].size(); i++){
            int adj = v[node][i];
            if(visited[adj] == true) continue;
            if(ans[pos-1].empty() && ans[pos+1].empty()){
                ans[pos+1].pb(adj);
                q.push({adj, pos+1});
                visited[adj] = true;
            }
            else if(ans[pos-1].empty()){
                for(int k = 0; k < ans[pos+1].size(); k++){
                    int lol = ans[pos+1][k];
                    auto it = find(all(v[lol]), adj);
                    if(it == v[lol].end());
                    else fail;
                }
                int flag = 0
                for(int k = 0; k < ans[pos+2].size(); k++){
                    int lol = ans[pos+2][k];
                    auto it = find(all(v[lol]), adj);
                    if(it == v[lol].end());
                    else flag = 1;
                }
                if(flag){
                    ans[pos+1].pb(adj);
                    q.push({adj,pos+1});
                }
                else{
                    ans[pos-1].pb(adj);
                    q.push({adj,pos-1});
                }
                visited[adj] = true;
            }
            else if(ans[pos+1].empty()){
                for(int k = 0; k < ans[pos-1].size(); k++){
                    int lol = ans[pos-1][k];
                    auto it = find(all(v[lol]), adj);
                    if(it == v[lol].end());
                    else fail;
                }
                int flag = 0;
                for(int k = 0; k < ans[pos-2].size(); k++){
                    int lol = ans[pos-2][k];
                    auto it = find(all(v[lol]), adj);
                    if(it == v[lol].end());
                    else flag = 1;
                }
                if(flag){
                    ans[pos-1].pb(adj);
                    q.push({adj,pos-1});
                }
                else{
                    ans[pos+1].pb(adj);
                    q.push({adj,pos+1});
                }
                visited[adj] = true;
            }
            else{
                int flag = 0;
                for(int k = 0; k < ans[pos+1].size(); k++){
                    int lol = ans[pos+1][k];
                    auto it = find(all(v[lol]), adj);
                    if(it == v[lol].end());
                    else flag = 1;
                }
                if(!flag){
                    ans[pos+1].pb(adj);
                    q.push({adj,pos+1});
                    visited[adj] = true;
                    continue;
                }
                int flag1 = 0;
                for(int k = 0; k < ans[pos-1].size(); k++){
                    int lol = ans[pos-1][k];
                    auto it = find(all(v[lol]), adj);
                    if(it == v[lol].end());
                    else flag1 = 1;
                }
                if(!flag1){
                    ans[pos-1].pb(adj);
                    q.push({adj,pos-1});
                    visited[adj] = true;
                    continue;
                }
                fail;
            }
        }
    }
    int cnt = 0;
    for(i = 0; i < ans.size(); i++) if(!ans[i].empty()) cnt++;
    cout << cnt << endl;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:84:17: error: expected ',' or ';' before 'for'
   84 |                 for(int k = 0; k < ans[pos+2].size(); k++){
      |                 ^~~
a.cc:84:32: error: 'k' was not declared in this scope
   84 |                 for(int k = 0; k < ans[pos+2].size(); k++){
      |                                ^