Pathfinding

No attempts yetTime limit1sMemory limit128 MB

Problem

Bessie is stranded on a deserted arctic island and wants to work out every route she could take back to her pasture. She has tested her boat and knows she can travel from one island to another in 1 unit of time whenever a current-driven route connects that ordered pair of islands.

She has mapped the ocean as single-hop routes between the $N$ ($1 \le N \le 100$) islands, numbered $1$ through $N$. Routes are one-way (unidirectional), because the currents push the boat in a fixed direction. A pair of islands may be joined by two separate routes using opposite currents, giving an effectively bidirectional link. No route ever connects an island to itself.

Given her starting island $M$ ($1 \le M \le N$) and the map, determine which islands are one hop away, which are two hops away, and so on. When several routes reach the same island, count only the shortest one.

For example, the following $N = 4$ islands are connected as shown, with $M = 1$:

start--> 1-------->2
         |         |
         |         |
         V         V
         4<--------3

Bessie reaches island 1 at time 0 (her start), islands 2 and 4 at time 1, and island 3 at time 2.

The map is given as a matrix $C$, where the entry in row $r$, column $c$ is $C_{rc}$ ($0 \le C_{rc} \le 1$). $C_{rc} = 1$ means the currents let Bessie travel directly from island $r$ to island $c$ in one time unit. Row $r$ has $N$ entries $C_{r1}, \dots, C_{rN}$.

Input

  • Line 1: two space-separated integers $N$ and $M$.
  • Lines 2 to $N+1$: line $r+1$ contains the $N$ space-separated integers of matrix row $r$, namely $C_{r1}, \dots, C_{rN}$.

Output

  • For each time $i = 0, 1, 2, \dots$, print one line listing, in ascending order, every island Bessie can first reach at exactly time $i$.
  • Print a line only while such islands exist; stop as soon as no island is first reached at the next time. Islands unreachable from $M$ are never printed.