using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5 2");
WillReturn.Add("11");
WillReturn.Add("01");
WillReturn.Add("11");
WillReturn.Add("10");
WillReturn.Add("00");
//2 3 2
}
else if (InputPattern == "Input2") {
WillReturn.Add("6 3");
WillReturn.Add("101");
WillReturn.Add("001");
WillReturn.Add("101");
WillReturn.Add("000");
WillReturn.Add("100");
WillReturn.Add("000");
//-1 3 3 -1
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int mGoalNode;
// 隣接リスト(正方向)
static Dictionary<int, List<int>> mToNodeListDictSei = new Dictionary<int, List<int>>();
// 隣接リスト(逆方向)
static Dictionary<int, List<int>> mToNodeListDictRev = new Dictionary<int, List<int>>();
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
mGoalNode = wkArr[0];
// 正方向の辺を貼る
for (int I = 1; I <= InputList.Count - 1; I++) {
string CurrStr = InputList[I];
mToNodeListDictSei[I] = new List<int>();
for (int J = 0; J <= CurrStr.Length - 1; J++) {
if (CurrStr[J] == '1') {
int ToNode = I + J + 1;
mToNodeListDictSei[I].Add(ToNode);
}
}
}
// 逆方向の辺を貼る
for (int I = 1; I <= InputList.Count - 1; I++) {
string CurrStr = InputList[I];
for (int J = 0; J <= CurrStr.Length - 1; J++) {
if (CurrStr[J] == '1') {
int FromNode = I + J + 1;
if (mToNodeListDictRev.ContainsKey(FromNode) == false) {
mToNodeListDictRev[FromNode] = new List<int>();
}
mToNodeListDictRev[FromNode].Add(I);
}
}
}
Dictionary<int, int> DistanceDictSei = ExecBFS(1, mToNodeListDictSei);
Dictionary<int, int> DistanceDictRev = ExecBFS(mGoalNode, mToNodeListDictRev);
// 使用不可になるノードをループさせ
// 使用不可になるノードの10ノード前から1ノード前を始点ノードとし、
// BFSの一部やり直し
var AnswerList = new List<int>();
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnqueue;
for (int I = 2; I <= mGoalNode - 1; I++) {
for (int J = I - 10; J <= I - 1; J++) {
if (DistanceDictSei.ContainsKey(J)) {
WillEnqueue.CurrNode = J;
WillEnqueue.Distance = DistanceDictSei[J];
Que.Enqueue(WillEnqueue);
}
}
bool FoundAnswer = false;
int Answer = int.MaxValue;
// ノード[最小コスト]なDict
var EdakiriDict = new Dictionary<int, int>();
while (Que.Count > 0) {
JyoutaiDef Dequeued = Que.Dequeue();
if (Dequeued.CurrNode > I) {
if (DistanceDictRev.ContainsKey(Dequeued.CurrNode)) {
int AnswerKouho = Dequeued.Distance + DistanceDictRev[Dequeued.CurrNode];
FoundAnswer = true;
Answer = Math.Min(Answer, AnswerKouho);
}
continue;
}
if (EdakiriDict.ContainsKey(Dequeued.CurrNode)) {
if (EdakiriDict[Dequeued.CurrNode] <= Dequeued.Distance) {
continue;
}
}
EdakiriDict[Dequeued.CurrNode] = Dequeued.Distance;
if (mToNodeListDictSei.ContainsKey(Dequeued.CurrNode)) {
foreach (int EachToNode in mToNodeListDictSei[Dequeued.CurrNode]) {
if (EachToNode == I) continue;
WillEnqueue.CurrNode = EachToNode;
WillEnqueue.Distance = Dequeued.Distance + 1;
Que.Enqueue(WillEnqueue);
}
}
}
if (FoundAnswer == false) {
AnswerList.Add(-1);
}
else {
AnswerList.Add(Answer);
}
}
Console.WriteLine(IntEnumJoin(" ", AnswerList.ToArray()));
}
// セパレータとInt型の列挙を引数として、結合したstringを返す
static string IntEnumJoin(string pSeparater, IEnumerable<int> pEnum)
{
string[] StrArr = Array.ConvertAll(pEnum.ToArray(), pX => pX.ToString());
return string.Join(pSeparater, StrArr);
}
// 隣接リストを引数として、BFSを行い、各ノードまでの距離を返す
static Dictionary<int, int> ExecBFS(int pStaNode, Dictionary<int, List<int>> pToNodeListDict)
{
var WillReturn = new Dictionary<int, int>();
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnqueue;
WillEnqueue.CurrNode = pStaNode;
WillEnqueue.Distance = 0;
Que.Enqueue(WillEnqueue);
var VisitedSet = new HashSet<int>();
VisitedSet.Add(pStaNode);
while (Que.Count > 0) {
JyoutaiDef Dequeued = Que.Dequeue();
WillReturn[Dequeued.CurrNode] = Dequeued.Distance;
if (pToNodeListDict.ContainsKey(Dequeued.CurrNode)) {
foreach (int EachToNode in pToNodeListDict[Dequeued.CurrNode]) {
if (VisitedSet.Add(EachToNode)) {
WillEnqueue.CurrNode = EachToNode;
WillEnqueue.Distance = Dequeued.Distance + 1;
Que.Enqueue(WillEnqueue);
}
}
}
}
return WillReturn;
}
struct JyoutaiDef
{
internal int CurrNode;
internal int Distance;
}
}