Some Hamiltonian paths

Consider a directed graph with nn vertices and all the n(n−1)n(n - 1) possible arcs, some of which are painted. How many Hamiltonian paths are in the graph starting at vertex 0, ending at vertex n−1n-1, and such that they do not traverse two consecutive painted arcs?

Input

Input consists of several cases. Every case begins with nn, followed by an n×nn \times n matrix, where the position (i,j)(i, j) has the color of the arc from vertex ii to vertex jj. A one indicates a painted arc, and a zero a non-painted arc. The diagonal (which is useless) only has zeroes. You can assume n≥2n \ge 2.

Output

For every case, print the number of permutations of the nn vertices that start at 0, end at n−1n - 1, and do not have three consecutive vertices xx, yy and zz such that the two arcs x→yx \to y and y→zy \to z are both painted. The test cases are such that the answer is smaller than 10610^6.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T11:50:24.126Z

© Jutge.org, 2006–2026.
https://jutge.org