{"trustable":true,"sections":[{"title":"","value":{"format":"MD","content":"Jane is one of the most talented young programmers as well as an astrophysicist. Recently she discovered a planet and named it Jotunheim - the world of giants. As you already guessed that the inhabitants are all giants. Among them the Frost Giants are the most evil ones. Before Jane could publicly announce her great discovery, the Frost Giants came and captured her in a maze. Since the Giants would be discovered to the universe because of her, that\u0027s why they lit fires on some positions in the maze to kill her.\n\nYou are given Jane\u0027s location in the maze and the positions of the fires lit by the Frost Giants whom are always keeping an eye on her; you must find out whether Jane can escape from the maze before fire catches her, and how fast she can do it.\n\nThe Maze is defined as a 2D grid and the locations are defined as squares. The cost of each move is one square per minute. In each move, Jane can move vertically or horizontally but not diagonally. She cannot move to a square which is blocked by an obstacle, or which is already burning. If a square has fire in it, in the next minute, fires spread to its adjacent **non-obstacle** squares (vertically or horizontally). Jane can escape from the maze from any squares that borders the edge of the maze."}},{"title":"Input","value":{"format":"MD","content":"Input starts with an integer **T (\u0026#8804; 50)**, denoting the number of test cases.\n\nThe first line of each test case contains the two integers **R** and **C**, separated by spaces, with **1 \u0026#8804; R, C \u0026#8804; 200** The following **R** lines of the test case each contain one row of the maze. Each of these lines contains exactly **C** characters, and each of these characters is one of:\n\n1. `#`, an obstacle.\n2. `.`, a free location.\n3. `J`, Jane\u0027s initial position in the maze (there will be exactly one `J` in the maze). \n4. `F`, position of a fire."}},{"title":"Output","value":{"format":"MD","content":"For each case, print the case number and `IMPOSSIBLE` if Jane cannot escape from the maze before fire reaches her, or the earliest time for Jane to safely escape from the maze, in minutes."}},{"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 5\n##.##\n#JF.#\n#...#\n#...#\n3 3\n###\n#J.\n#.F\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003eCase 1: 3\nCase 2: IMPOSSIBLE\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}