{"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 Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon\u0027s home. But he can\u0027t bring his precious with him. As you know, uncle Vernon never allows such magic things in his house. So Harry has to deposit his precious in the Gringotts Wizarding Bank which is owned by some goblins. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:\u003cbr\u003e\u003cbr\u003e\u003ccenter\u003e\u003cimg style\u003d\"max-width:100%;\" src\u003d\"CDN_BASE_URL/00e39508a961e23ed2da15ef6ef9264a?v\u003d1715917563\"\u003e\u003c/center\u003e \u003cbr\u003e\u003cbr\u003e Some rooms are indestructible and some rooms are vulnerable. Goblins always care more about their own safety than their customers\u0027 properties, so they live in the indestructible rooms and put customers\u0027 properties in vulnerable rooms. Harry Potter\u0027s precious are also put in some vulnerable rooms. Dudely wants to steal Harry\u0027s things this holiday. He gets the most advanced drilling machine from his father, uncle Vernon, and drills into the bank. But he can only pass though the vulnerable rooms. He can\u0027t access the indestructible rooms. He starts from a certain vulnerable room, and then moves in four directions: north, east, south and west. Dudely knows where Harry\u0027s precious are. He wants to collect all Harry\u0027s precious by as less steps as possible. Moving from one room to another adjacent room is called a \u0027step\u0027. Dudely doesn\u0027t want to get out of the bank before he collects all Harry\u0027s things. Dudely is stupid.He pay you $1,000,000 to figure out at least how many steps he must take to get all Harry\u0027s precious.\u003c/div\u003e"}},{"title":"Input","value":{"format":"HTML","content":" There are several test cases.\u003cbr\u003e In each test cases:\u003cbr\u003e The first line are two integers N and M, meaning that the bank is a N × M grid(0\u0026lt;N,M \u0026lt;\u003d 100).\u003cbr\u003e Then a N×M matrix follows. Each element is a letter standing for a room. \u0027#\u0027 means a indestructible room, \u0027.\u0027 means a vulnerable room, and the only \u0027@\u0027 means the vulnerable room from which Dudely starts to move.\u003cbr\u003e The next line is an integer K ( 0 \u0026lt; K \u0026lt;\u003d 4), indicating there are K Harry Potter\u0027s precious in the bank.\u003cbr\u003e In next K lines, each line describes the position of a Harry Potter\u0027s precious by two integers X and Y, meaning that there is a precious in room (X,Y).\u003cbr\u003e The input ends with N \u003d 0 and M \u003d 0\u003cbr\u003e"}},{"title":"Output","value":{"format":"HTML","content":" For each test case, print the minimum number of steps Dudely must take. If Dudely can\u0027t get all Harry\u0027s things, print -1.\u003cbr\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 3\r\n##@\r\n#.#\r\n1\r\n2 2\r\n4 4\r\n#@##\r\n....\r\n####\r\n....\r\n2\r\n2 1\r\n2 4\r\n0 0\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e-1\r\n5\r\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}