{"trustable":true,"prependHtml":"\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS-MML_HTMLorMML\" type\u003d\"text/javascript\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cdiv class\u003d\"panel_content\"\u003e 《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng\u0027en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang Monk to India to get sacred Buddhism texts. \u003cbr\u003e\u003cbr\u003e During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.\u003cbr\u003e\u003cbr\u003e Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace. But to rescue Tang Monk, Sun Wukong might need to get some keys and kill some snakes in his way.\u003cbr\u003e\u003cbr\u003e The palace can be described as a matrix of characters. Each character stands for a room. In the matrix, \u0027K\u0027 represents the original position of Sun Wukong, \u0027T\u0027 represents the location of Tang Monk and \u0027S\u0027 stands for a room with a snake in it. Please note that there are only one \u0027K\u0027 and one \u0027T\u0027, and at most five snakes in the palace. And, \u0027.\u0027 means a clear room as well \u0027#\u0027 means a deadly room which Sun Wukong couldn\u0027t get in.\u003cbr\u003e\u003cbr\u003e There may be some keys of different kinds scattered in the rooms, but there is at most one key in one room. There are at most 9 kinds of keys. A room with a key in it is represented by a digit(from \u00271\u0027 to \u00279\u0027). For example, \u00271\u0027 means a room with a first kind key, \u00272\u0027 means a room with a second kind key, \u00273\u0027 means a room with a third kind key... etc. To save Tang Monk, Sun Wukong must get ALL kinds of keys(in other words, at least one key for each kind).\u003cbr\u003e\u003cbr\u003e For each step, Sun Wukong could move to the adjacent rooms(except deadly rooms) in 4 directions(north, west, south and east), and each step took him one minute. If he entered a room in which a living snake stayed, he must kill the snake. Killing a snake also took one minute. If Sun Wukong entered a room where there is a key of kind N, Sun would get that key if and only if he had already got keys of kind 1,kind 2 ... and kind N-1. In other words, Sun Wukong must get a key of kind N before he could get a key of kind N+1 (N\u0026gt;\u003d1). If Sun Wukong got all keys he needed and entered the room in which Tang Monk was cuffed, the rescue mission is completed. If Sun Wukong didn\u0027t get enough keys, he still could pass through Tang Monk\u0027s room. Since Sun Wukong was a impatient monkey, he wanted to save Tang Monk as quickly as possible. Please figure out the minimum time Sun Wukong needed to rescue Tang Monk.\u003c/div\u003e"}},{"title":"Input","value":{"format":"HTML","content":" There are several test cases.\u003cbr\u003e\u003cbr\u003e For each case, the first line includes two integers N and M(0 \u0026lt; N \u0026lt;\u003d 100, 0\u0026lt;\u003dM\u0026lt;\u003d9), meaning that the palace is a N×N matrix and Sun Wukong needed M kinds of keys(kind 1, kind 2, ... kind M). \u003cbr\u003e\u003cbr\u003e Then the N × N matrix follows.\u003cbr\u003e\u003cbr\u003e The input ends with N \u003d 0 and M \u003d 0."}},{"title":"Output","value":{"format":"HTML","content":" For each test case, print the minimum time (in minutes) Sun Wukong needed to save Tang Monk. If it\u0027s impossible for Sun Wukong to complete the mission, print \"impossible\"(no quotes). "}},{"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\u003e3 1\r\nK.S\r\n##1\r\n1#T\r\n3 1\r\nK#T\r\n.S#\r\n1#.\r\n3 2\r\nK#T\r\n.S.\r\n21.\r\n0 0\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e5\r\nimpossible\r\n8\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}