Farmer John has built a beautiful rectangular pond for his cows to admire and exercise in. The pond is divided into a grid of $M$ rows and $N$ columns ($1 \le M \le 30$; $1 \le N \le 30$). Some cells hold remarkably sturdy lilypads, some hold rocks, and the rest are open water.
Bessie practices her ballet by jumping from lilypad to lilypad. She currently stands on one lilypad and wants to reach another. Every jump Bessie makes is exactly a chess knight's move: one cell in one direction and two cells in the perpendicular direction (or two cells in one direction and one in the perpendicular direction). She may only land on lilypads, never on open water or rocks.
Sometimes Bessie cannot reach her destination because some intermediate lilypads are missing. Ever thrifty, Farmer John wants to add as few new lilypads as possible so that a sequence of knight jumps can carry Bessie from her starting lilypad to her destination lilypad. A new lilypad may be placed only on an open-water cell, never on a rock.
Help Farmer John determine, in order:
0 — open water1 — an existing lilypad2 — a rock3 — the lilypad Bessie starts on4 — the lilypad Bessie wants to reachThere is exactly one 3 and exactly one 4.
-1 and nothing else.-1.-1.In the sample pond, two lilypads must be added; the two possible placements are marked with x below:
0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0
0 x 0 0 0 2 0 1 0 0 0 0 0 2 0 1
0 0 0 0 x 4 0 0 0 0 x 0 x 4 0 0
3 0 0 0 0 0 1 0 3 0 0 0 0 0 1 0
With those additions Bessie needs at least $6$ jumps, and there are exactly two distinct $6$-jump paths, labelled A through G below:
0 0 0 C 0 0 0 0 0 0 0 C 0 0 0 0
0 B 0 0 0 2 0 F 0 0 0 0 0 2 0 F
0 0 0 0 D G 0 0 0 0 B 0 D G 0 0
A 0 0 0 0 0 E 0 A 0 0 0 0 0 E 0