Hamiltonian Circuit

Insert vertices one by one into a directed cycle in a tournament by the two given rules, and print -1 when no rule applies.

Medium7GraphSimulationNo attempts yetTime limit1.5sMemory limit512 MB

Problem

A directed graph GG has NN vertices and N(N1)/2N(N-1)/2 edges. The vertices are numbered 0 to N1N-1. For two distinct vertices ii and jj, exactly one of the edge from ii to jj and the edge from jj to ii is present, so GG is a complete graph once you ignore the directions.

XX is the adjacency matrix of GG. Xi,jX_{i,j} is + when the edge from ii to jj is present and - when it is not. Xi,iX_{i,i} is always ..

A Hamiltonian circuit of GG is a cycle of length NN that passes through every vertex of GG once. Given GG, decide whether a Hamiltonian circuit exists, and print one if it does.

Input

The first line has the number of vertices NN. Each of the next NN lines has one row of the adjacency matrix XX. The jj-th character of the ii-th of those lines is Xi,jX_{i,j}.

Output

If GG has no Hamiltonian circuit, print -1.

Otherwise print the NN vertices of a Hamiltonian circuit in the order they are passed, separated by one space. A graph can have several Hamiltonian circuits, so print exactly the one that the procedure below builds.

Keep a sequence c0,c1,,ck1c_0, c_1, \dots, c_{k-1} of distinct vertices. The sequence stands for the cycle c0c1ck1c0c_0 \to c_1 \to \dots \to c_{k-1} \to c_0, and every neighbouring pair, including the pair (ck1,c0)(c_{k-1}, c_0), is an edge of GG. Start from the sequence that holds vertex 0 alone and repeat the following until the sequence holds all NN vertices. A vertex outside the sequence is called free.

  1. If a free vertex vv has both an edge from a sequence vertex to vv and an edge from vv to a sequence vertex, take the smallest such vv. Find the smallest index ii with Xci,vX_{c_i,v} equal to + and Xv,c(i+1)modkX_{v,c_{(i+1) \bmod k}} equal to +, then put vv directly after cic_i. Such an index ii always exists.
  2. Otherwise, collect the pairs (w,u)(w, u) of free vertices where every sequence vertex has an edge to ww, uu has an edge to every sequence vertex, and the edge from ww to uu is present. Take the smallest such pair in lexicographic order and append ww and then uu to the end of the sequence.

When GG has a Hamiltonian circuit, one of the two rules applies at every turn, and vertex 0 stays in front of the sequence, so the printed circuit starts at vertex 0.

Constraints

  • 3N10003 \le N \le 1\,000