atcoder#ABC291F. [ABC291F] Teleporter and Closed off

[ABC291F] Teleporter and Closed off

Score : 500500 points

Problem Statement

There are NN cities numbered city 11, city 22, \ldots, and city NN. There are also one-way teleporters that send you to different cities. Whether a teleporter can send you directly from city ii (1iN)(1\leq i\leq N) to another is represented by a length-MM string SiS_i consisting of 0 and 1. Specifically, for 1jN1\leq j\leq N,

  • if 1jiM1\leq j-i\leq M and the (ji)(j-i)-th character of SiS_i is 1, then a teleporter can send you directly from city ii to city jj;
  • otherwise, it cannot send you directly from city ii to city jj.

Solve the following problem for k=2,3,,N1k=2,3,\ldots, N-1:

Can you travel from city 11 to city NN without visiting city k by repeatedly using a teleporter? If you can, print the minimum number of times you need to use a teleporter; otherwise, print 1-1.

Constraints

  • 3N1053 \leq N \leq 10^5
  • 1M101\leq M\leq 10
  • $M
  • SiS_i is a string of length MM consisting of 0 and 1.
  • If i+j>Ni+j>N, then the jj-th character of SiS_i is 0.
  • NN and MM are integers.

Input

The input is given from Standard Input in the following format:

NN MM

S1S_1

S2S_2

\vdots

SNS_N

Output

Print (N2)(N-2) integers, separated by spaces, in a single line. The ii-th (1iN2)(1\leq i\leq N-2) integer should be the answer to the problem for k=i+1k=i+1.

5 2
11
01
11
10
00
2 3 2

A teleporter sends you

  • from city 11 to cities 22 and 33;
  • from city 22 to city 44;
  • from city 33 to cities 44 and 55;
  • from city 44 to city 55; and
  • from city 55 to nowhere.

Therefore, there are three paths to travel from city 11 to city 55:

  • path 11 : city 11 \to city 22 \to city 44 \to city 55;
  • path 22 : city 11 \to city 33 \to city 44 \to city 55; and
  • path 33 : city 11 \to city 33 \to city 55.

Among these paths,

  • two paths, path 22 and path 33, do not visit city 22. Among them, path 33 requires the minimum number of teleporter uses (twice).
  • Path 11 is the only path without city 33. It requires using a teleporter three times.
  • Path 33 is the only path without city 44. It requires using a teleporter twice.

Thus, 22, 33, and 22, separated by spaces, should be printed.

6 3
101
001
101
000
100
000
-1 3 3 -1

The only path from city 11 to city 66 is city 11 \to city 22 \to city 55 \to city 66. For k=2,5k=2,5, there is no way to travel from city 11 to city 66 without visiting city kk. For k=3,4k=3,4, the path above satisfies the condition; it requires using a teleporter three times.

Thus, 1-1, 33, 33, and 1-1, separated by spaces, should be printed.

Note that a teleporter is one-way; a teleporter can send you from city 33 to city 44, but not from city 44 to city 33, so the following path, for example, is invalid: city 11 \to city 44 \to city 33 \to city 66.