{"trustable":true,"sections":[{"title":"","value":{"format":"MD","content":"You may have played the game \u0027Blade and Sword\u0027, it\u0027s an action game. However, in this problem we are actually solving one stage of the game.\n\nFor simplicity, assume that the game stage can be modeled as a **2D** grid which has **m** rows and **n** columns. The cells are categorized as follows:\n\n* `.` represents an empty space. The player can move through it.\n* `#` represents wall, and the player cannot move through it. You can assume that the boundaries of the grid will be walls.\n* `*` means a teleporting cell, once the player moves into this cell, he must choose **any other** teleporting cell where he will be taken to. But if he cannot find a desired teleporting cell, he will die. However, after moving to the desired teleporting cell, he can either move to an adjacent cell, or he can teleport again using the same procedure.\n* `P` means the position of the player and there will be exactly one cell containing `P`.\n* `D` means the destination cell and there will be exactly one cell containing `D`.\n\nNow you are given a stage and the player starts moving. It takes one unit of time for the player to move to any adjacent cell from his current position. Two cells are adjacent if they share a side. One unit of time is needed for the teleporting service; that means taking the player from one teleporting cell to any other teleporting cell. Your task is to find the minimum possible time unit required for the player to reach the destination cell."}},{"title":"Input","value":{"format":"MD","content":"Input starts with an integer **T (\u0026#8804; 100)**, denoting the number of test cases.\n\nEach case starts with a line containing two integers: **m** and **n (3 \u0026#8804; m, n \u0026#8804; 200)**. Each of the next **m** lines contains **n** characters denoting the stage. The given stage follows the restrictions stated above."}},{"title":"Output","value":{"format":"MD","content":"For each case, print the case number and the minimum required time. If it\u0027s impossible for the player to reach the destination cell, print `impossible`. See the samples for details."}},{"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\n4 10\n##########\n#.P..#*..#\n#*......D#\n##########\n3 9\n#########\n#P.#..D.#\n#########\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003eCase 1: 6\nCase 2: impossible\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}