{"trustable":true,"sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eOn Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The purpose of the game is to lead the stone from the start to the goal with the minimum number of moves.\u003c/p\u003e\u003cp\u003eFig. 1 shows an example of a game board. Some squares may be occupied with blocks. There are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct.) Once the stone begins to move, it will proceed until it hits a block. In order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again.\u003c/p\u003e\u003cp align\u003d\"center\"\u003e\u003cimg src\u003d\"CDN_BASE_URL/dfcadffc211686655c382e9ad94bdbfe?v\u003d1715003543\"\u003e\u003cbr\u003eFig. 1: Example of board (S: start, G: goal)\u003c/p\u003e\u003cp\u003eThe movement of the stone obeys the following rules:\u003c/p\u003e\u003cul\u003e\u003cli\u003eAt the beginning, the stone stands still at the start square. \u003c/li\u003e\u003cli\u003eThe movements of the stone are restricted to x and y directions. Diagonal moves are prohibited. \u003c/li\u003e\u003cli\u003eWhen the stone stands still, you can make it moving by throwing it. You may throw it to any direction unless it is blocked immediately(Fig. 2(a)). \u003c/li\u003e\u003cli\u003eOnce thrown, the stone keeps moving to the same direction until one of the following occurs: \u003cul\u003e\u003cli\u003eThe stone hits a block (Fig. 2(b), (c)). \u003cul\u003e\u003cli\u003eThe stone stops at the square next to the block it hit. \u003c/li\u003e\u003cli\u003eThe block disappears. \u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003cli\u003eThe stone gets out of the board. \u003cul\u003e\u003cli\u003eThe game ends in failure. \u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003cli\u003eThe stone reaches the goal square. \u003cul\u003e\u003cli\u003eThe stone stops there and the game ends in success. \u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003c/ul\u003e\u003c/li\u003e\u003cli\u003eYou cannot throw the stone more than 10 times in a game. If the stone does not reach the goal in 10 moves, the game ends in failure.\u003c/li\u003e\u003c/ul\u003e\u003cp align\u003d\"center\"\u003e\u003cimg src\u003d\"CDN_BASE_URL/725189cee19b86e1cd2410490b0dadc2?v\u003d1715003543\"\u003e\u003cbr\u003eFig. 2: Stone movements\u003c/p\u003e\u003cp\u003eUnder the rules, we would like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required.\u003c/p\u003e\u003cp\u003eWith the initial configuration shown in Fig. 1, 4 moves are required to bring the stone from the start to the goal. The route is shown in Fig. 3(a). Notice when the stone reaches the goal, the board configuration has changed as in Fig. 3(b).\u003c/p\u003e\u003cp align\u003d\"center\"\u003e\u003cimg src\u003d\"CDN_BASE_URL/c657651fda754a877e0c3a2ce3a06fd7?v\u003d1715003543\"\u003e\u003cbr\u003eFig. 3: The solution for Fig. D-1 and the final board configuration\u003c/p\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003eThe input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. The number of datasets never exceeds 100.\u003c/p\u003e\u003cp\u003eEach dataset is formatted as follows.\u003c/p\u003e\u003cblockquote\u003e\u003cp\u003e\u003ci\u003ethe width(\u003dw) and the height(\u003dh) of the board\u003c/i\u003e \u003cbr\u003e\u003ci\u003eFirst row of the board\u003c/i\u003e \u003cbr\u003e... \u003cbr\u003e\u003ci\u003eh-th row of the board\u003c/i\u003e\u003c/p\u003e\u003c/blockquote\u003e\u003cp\u003eThe width and the height of the board satisfy: 2 \u0026lt;\u003d \u003ci\u003ew\u003c/i\u003e \u0026lt;\u003d 20, 1 \u0026lt;\u003d \u003ci\u003eh\u003c/i\u003e \u0026lt;\u003d 20.\u003c/p\u003e\u003cp\u003eEach line consists of \u003ci\u003ew\u003c/i\u003e decimal numbers delimited by a space. The number describes the status of the corresponding square.\u003c/p\u003e\u003cblockquote\u003e\u003ctable id\u003d\"table1\"\u003e\u003ctbody\u003e\u003ctr\u003e\u003ctd\u003e0 \u003c/td\u003e\u003ctd\u003evacant square\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003e1 \u003c/td\u003e\u003ctd\u003eblock\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003e2 \u003c/td\u003e\u003ctd\u003estart position\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003e3 \u003c/td\u003e\u003ctd\u003egoal position\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\u003c/table\u003e\u003c/blockquote\u003e\u003cp\u003eThe dataset for Fig. D-1 is as follows:\u003c/p\u003e\u003cblockquote\u003e\u003cp\u003e6 6 \u003cbr\u003e1 0 0 2 1 0 \u003cbr\u003e1 1 0 0 0 0 \u003cbr\u003e0 0 0 0 0 3 \u003cbr\u003e0 0 0 0 0 0 \u003cbr\u003e1 0 0 0 0 1 \u003cbr\u003e0 1 1 1 1 1\u003c/p\u003e\u003c/blockquote\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003eFor each dataset, print a line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. If there are no such routes, print -1 instead. Each line should not have any character other than this number.\u003c/p\u003e"}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e2 1\r\n3 2\r\n6 6\r\n1 0 0 2 1 0\r\n1 1 0 0 0 0\r\n0 0 0 0 0 3\r\n0 0 0 0 0 0\r\n1 0 0 0 0 1\r\n0 1 1 1 1 1\r\n6 1\r\n1 1 2 1 1 3\r\n6 1\r\n1 0 2 1 1 3\r\n12 1\r\n2 0 1 1 1 1 1 1 1 1 1 3\r\n13 1\r\n2 0 1 1 1 1 1 1 1 1 1 1 3\r\n0 0\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e1\r\n4\r\n-1\r\n4\r\n10\r\n-1\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}