Traffic Lanes

No attempts yetTime limit1sMemory limit128 MB

Problem

Long ago, a highway with nn lanes was built in Bytocia (Bajtocja), connecting the two cities AA and BB. The road became so heavily travelled that some of its fragments turned impassable.

Bajtek, a resident of city AA, does not like turning the steering wheel too many times. He wonders how to reach city BB while changing lanes as few times as possible.

Bajtek may start his journey from any lane and may also finish on any lane. Under the highway traffic rules, making a U-turn or reversing is not allowed.

Input

The first line of standard input contains two integers nn and mm (1n,m10001 \le n, m \le 1000). Each of the next nn lines describes one lane, in order. Each such line contains mm integers: 0 marks a passable fragment of the highway and 1 marks an impassable one.

Output

Print a single integer, the minimum number of lane changes, on the first and only line of standard output. If crossing the highway is impossible, print the single word NIE instead.