Videogames are an indispensable part of the life of Noldo. So much so, that he can even delay the presentation of the final project of his degree for a good time playing. In this problem you must solve the videogame that Noldo was playing the day he should have been defending his project.
You must traverse a rectangular grid from an initial position to a final position in the shortest possible time. Each square has a room or an obstacle. You cannot pass through obstacles, or leave off the grid. Each room has a single exit door, initially oriented to the north, east, south or west. After each time unit, all doors move 90 degrees clockwise (for example, a door first oriented to the east, will be oriented to the south, then to the west, etcetera). When you are in a room, you can choose to wait for a time unit, or to move to the room adjacent to the exit door, also spending a time unit.
Input begins with the number of test cases. Every case begins with the number of rows and the number of columns of the map. Each of the following lines contain the characters of a row, with the following convention:
‘I’: initial room, oriented to the north.
‘F’: final room.
‘X’: square blocked by an obstacle.
‘N’: room initially oriented to the north.
‘E’: room initially oriented to the east.
‘S’: room initially oriented to the south.
‘W’: room initially oriented to the west.
Each grid has exactly one ‘I’ and one
‘F’.
For every given map, print the case number, followed by the minimum time required to go from the origin to destination. If it is impossible, tell so. Follow the format of the example.
Input
8 4 4 XXXX XFSX XINX XXXX 2 1 I F 2 2 IE XF 2 2 IE SF 1 3 FWI 1 3 FNI 4 5 XXXXX XNXFX XIXSX XXXXX 4 7 NSEWSNE EESWNWI SSNNEEW FSEWNSW
Output
#1: 1 #2: 3 #3: 6 #4: 4 #5: 5 #6: 8 #7: impossible #8: 14