Break a Prison
InterviewTime limit2sMemory limit1024 MB
Find the fewest moves from S to E on a grid where two consecutive moves may not turn right, and surveillance cells are blocked.
Problem
Jennifer is a software engineer at a tech company. Her company decided to join the ICPC (Inter-Company Prison breaking Contest), and she was chosen as the company's representative.
In the ICPC, every participant must escape from a prison. The prison is an grid with rows and columns of rooms. Room is the room in row and column . Two rooms and are adjacent if and only if . There is an unlocked door between every pair of adjacent rooms. Some rooms are under surveillance, and a participant can enter a room only if it is not under surveillance. The starting room and the exit room are guaranteed to be free of surveillance.
The CEO asked Jennifer not to turn right during the contest. That is, there must not be two consecutive moves that satisfy the following condition.
Condition: Suppose Jennifer moves from room to and then to . The move is forbidden if .

Figure B.1. Example of allowed and denied moves
In Figure B.1, if the last move follows the dashed arrow, Jennifer cannot move downward, but she can move in the other three directions.
U-turns are allowed.
As Jennifer's colleague, write a program that finds the minimum number of moves needed to reach the exit.
Input
The first line contains and (). Each of the next lines contains characters. The character describes room :
- S: the starting room
- E: the exit room
- .: a room not under surveillance
- #: a room under surveillance
The characters S and E each appear exactly once.
Output
Print the minimum number of moves needed to reach the exit. If the exit cannot be reached, print .
Hint
The optimal route for the third sample is shown below.

Figure B.2. The optimal route in Sample Input 3