SP13156 MAY99_1 - Tom and Jerry

Description

Tom and Jerry is a favourite cartoon of many of us . One day Manku was sitting watching an episode of Tom and Jerry where he found that Tom and Jerry both entered a rectangular maze and Tom was after Jerry , but Jerry being the hero , returned safely. Manku then start making different scenarios and wondering that if Jerry moves optimally and Tom knows entire path that Jerry is expected to take, then will Jerry be able to escape out of the maze or not ?? Moreover Manku added 1 rule to this that if Jerry moves from position A to position B , then at any cost he is not allowed to return from position B to position A. Jerry , if is at the escape positions of the maze, in the beginning , then he can't exit from that same position . Moreover he can't escape if he is caught by Tom at any position . Jerry and Tom can move up , down , left , right or wait on their position. If it is guaranteed that either Jerry would escape or Tom would catch him . All characters at row = 0 or row = m-1 or column = 0 or column = n-1 , which are '.' or 'J' or 'T' are escape positions.

Input Format

Each input file consist of only 1 test case. 1st line of input contains 2 no's m and n , both integers are less than or equal to 100 , the size of the rectangular maze. Then m lines follows containing n characters each. . means an open space so that tom or jerry can move there \# means a closed place T means starting position of Tom J means starting position of Jerry

Output Format

Output single line containing a character W and integer D where W is 'J' if Jerry can escape or else 'T' and D is the minimum time taken by Jerry to escape ( if W is 'J') or maximum time for which Jerry is alive ( if W is 'T')