p01142 Karakuri Doll
Karakuri Doll

Karakuri doll

English text is not available in this practice contest.

After many years of research, Karakuri puppeteer JAG has succeeded in developing a wonderful tea-drawing doll that combines traditional and latest techniques. This tea-drawing doll is placed in a teacup with tea in the kitchen (K) in a house divided by a grid as shown in the figure below, and it is taken to the place of the master (M), and the master takes it to the place of the master (M). The job is to return to the kitchen when you put the cup of tea you have finished drinking. However, this doll is not smart enough to find the round-trip route between the kitchen and the master. The user needs to pre-populate this doll with a program so that the doll can reciprocate properly.

Example of house structure
Figure F-1: Example of house structure

There are two commands that the doll accepts, "turn left" and "turn right", and the programs that can be input to the doll are a finite sequence of commands. After leaving the kitchen, the doll advances until it hits a wall (the square represented by "#" in the figure), and when it hits the wall, it turns left or right according to the first command. After that, it moves forward until it hits the wall again, and when it hits, it executes the next command, and things proceed in the same way. If you still hit a wall and cannot move forward after changing direction, execute the next command without moving from that location. When it hits a wall, the doll will stop if there are no commands left to execute. This is called sequential execution of the program.

When returning to the kitchen from the master's whereabouts, the instruction sequence of the program is executed in reverse order, and the direction is changed to the left and right. This is called reverse execution of the program. The behavior other than the instruction execution order and direction change is the same as the forward execution.

For example, in the house shown above, if you give this doll a program of "right, right, left", on the outbound route, point a will turn to the right, point b will turn to the right, and point c will turn to the left. Arrive at. On the return trip, the direction is changed to the right at point c, to the left at point b, and to the left at point a, and returns to the kitchen. On the other hand, when the program "left, left" is given, the doll first turns to the left at point a. He tries to go straight, but he hits the wall on the left side, so he turns to the left according to the second command while staying at point a. Then, when I went straight and came back to the kitchen, I hit a wall and stopped because there were no commands left.

By the way, Mr. JAG made a big mistake. In fact, there are cases where no matter what kind of program is given, it is not possible to reach the whereabouts of the master. Also, even if you arrive at your husband's place, you may not be able to bring the cup to the kitchen.

Your job is to write a program to determine if this doll can reach your husband's whereabouts and return to the kitchen when you use it in a house.

Input

The input consists of multiple datasets, with the last line written 0 0 indicating the end of the input. The number of data sets is 128 or less.

The first line of each dataset contains two space-separated integers W and H that represent the size of the house. These integers satisfy 3 ≤ W ≤ 64, 3 ≤ H ≤ 16. The following H line means the structure of the room. Each line consists of a W character, where "#` "indicates the wall," `.`" (Period) indicates the floor, "` K` "indicates the kitchen, and" `M`" indicates the location of the master. .. Characters other than these are not included.

The outermost part of this house is always guaranteed to be a wall ("` # `"). Also, there is always one "` K` "and" `M`" for each dataset, with three directions being walls ("` # `") and the remaining one direction being floors ("`. `"). ).

Output

For each dataset, output the message on one line as described below.

* If you do not arrive at your husband's whereabouts no matter what program you give, "He cannot bring tea to his master."
* If you arrive at your husband's whereabouts but cannot program to return to the kitchen afterwards, "He cannot return to the kitchen."
* If you can program to arrive at your husband's whereabouts and return to the kitchen, "He can accomplish his mission."



Here, arriving at the master's whereabouts means that the doll is present at the master's whereabouts at the time of stopping when the program is executed in order from the state where the doll is placed in the kitchen and turned in the direction without a wall. Returning to the kitchen means that the doll is present in the kitchen at the time of stop when the program is run backwards from the state where the doll is placed in the owner's place and turned in the direction without a wall. The orientation of the doll at the time of stop does not matter. In addition, both the outbound and inbound routes may pass through the kitchen or the master in the middle of the route. However, even if the doll passes through the kitchen or the master in the middle of the route, if the doll is elsewhere at the time of stop, it is not considered to have arrived at the master's whereabouts or returned to the kitchen.

Sample Input


5 3

K.M #

9 5

..... ###
. ### .. M #
K #######

9 5

K ...... #
.####
M ####

9 5

M ...... #
.####
K ####

7 9

M #
. #
..... #
. ###. #
..... #
. #####
K #####

7 6

. #
.... #
K .. #. #
M #. #

7 8

... ##
. # M #
..... #
. # ... #
. # .. ##
.K ####

9 6

.. ##. #
...... #
K .... #. #
.. # M #. #

9 6

. #######
.... # M. #
. # ... #. #
K # ​​... #

12 7

...####. #
K # ​​... M ##. #
..... # ... #
........ #. #
.. # ... #. #

23 16

...############
. ###. ########
..... ########
. #. #. ### ... ##
. #. #. ######. ###
............ ###
. ###. ######. ###
.######. ###
K ....... #####. ###
. #######. ######. ###
. #######. ######. ###
................. M #
. #######. ##########
...##### ... #########

46 16

.............. # .. ##############################
.. # .......... #. #. ### .... # ......... #
................. ###. #. #. # ............ # .. #
... # ... # .... # ... #. ###. # ...................... #
... # .... # .... #. #. ###. #. # ... # .... # .... # .... # ... #
. # .......... # .......... #. #. # .... # .... # .... # ... #
... # ... # ....... ###. # ........ # ......... # ... #. #
. # ... # ......... ###. # .. ## ....... # ........ # ... #
... # ........ # .. ###. # .. ##......... # ........ #. #
............... ###. # .. ## .. # ............ # ... # ... #
.######. # .. ## ..................... #
K ....... #. ########. ############
............... M ###########
....... #######################

0 0


Output for the Sample Input


He can accomplish his mission.
He can accomplish his mission.
He cannot bring tea to his master.
He cannot return to the kitchen.
He can accomplish his mission.
He can accomplish his mission.
He cannot return to the kitchen.
He cannot return to the kitchen.
He can accomplish his mission.
He can accomplish his mission.
He cannot return to the kitchen.
He can accomplish his mission.






Example

Input

5 3
#####
#K.M#
#####
9 5
#########
#.....###
#.###..M#
#K#######
#########
9 5
#########
#K......#
####.####
####M####
#########
9 5
#########
#M......#
####.####
####K####
#########
7 9
#######
#####M#
#####.#
#.....#
#.###.#
#.....#
#.#####
#K#####
#######
7 6
#######
#####.#
##....#
#K..#.#
###M#.#
#######
7 8
#######
##...##
###.#M#
#.....#
#.#...#
#.#..##
#.K####
#######
9 6
#########
###..##.#
##......#
#K....#.#
##..#M#.#
#########
9 6
#########
#.#######
#....#M.#
#.#...#.#
###K#...#
#########
12 7
############
###...####.#
##K#...M##.#
##.....#...#
#........#.#
###..#...#.#
############
23 16
#######################
#########...###########
##########.###.########
##########.....########
##########.#.#.###...##
########.#.#.######.###
########............###
########.###.######.###
############.######.###
#K...........######.###
####.#######.######.###
####.#######.######.###
####.................M#
####.#######.##########
###...#####...#########
#######################
46 16
##############################################
#..............#..############################
#..#..........#.#.###....#...................#
#.................###.#.#.#...............#..#
#...#..#....#...#.###.#......................#
#...#....#....#.#.###.#.#...#....#....#..#...#
#.#........#..........#.#.#....#....#....#...#
#...#...#.......###.#........#.........#...#.#
#.#...#.........###.#..##.......#........#...#
#...#........#..###.#..##.........#........#.#
#...............###.#..##..#.........#...#...#
############.######.#..##....................#
###########K...........#.########.############
###################...............M###########
##################.......#####################
##############################################
0 0


Output

He can accomplish his mission.
He can accomplish his mission.
He cannot bring tea to his master.
He cannot return to the kitchen.
He can accomplish his mission.
He can accomplish his mission.
He cannot return to the kitchen.
He cannot return to the kitchen.
He can accomplish his mission.
He can accomplish his mission.
He cannot return to the kitchen.
He can accomplish his mission.
```python
from collections import deque
import sys
readline = sys.stdin.readline
write = sys.stdout.write
def solve():
    W, H = map(int, readline().split())
    if W == H == 0:
        return False
    S = [readline().strip() for i in range(H)]

    dd = ((-1, 0), (0, -1), (1, 0), (0, 1))

    sx = sy = gx = gy = 0
    for i in range(H):
        Si = S[i]
        for j in range(W):
            if Si[j] == "K":
                sy = i; sx = j
            elif Si[j] == "M":
                gy = i; gx = j
    ks = kg = 0
    for k in range(4):
        dx, dy = dd[k]
        if S[sy+dy][sx+dx] != "#":
            ks = k
        if S[gy+dy][gx+dx] != "#":
            kg = k

    dx, dy = dd[ks]
    sx1 = sx; sy1 = sy
    while S[sy1+dy][sx1+dx] != "#":
        sx1 += dx; sy1 += dy
    dx, dy = dd[kg]
    gx1 = gx; gy1 = gy
    while S[gy1+dy][gx1+dx] != "#":
        gx1 += dx; gy1 += dy

    N = W*H*4
    G0 = [[[] for i in range(2)] for j in range(N)]
    G1 = [[[] for i in range(2)] for j in range(N)]
    for i in range(H):
        for j in range(W):
            if S[i][j] == "#":
                continue
            for k in range(4):
                dx, dy = dd[k]
                x = j; y = i
                while S[y+dy][x+dx] != "#":
                    x += dx; y += dy
                dx1, dy1 = dd[k-1]
                if S[i+dy1][j+dx1] == "#":
                    p0 = (i*W + j)*4 + (k-1)%4
                    p1 = (y*W + x)*4 + k
                    G0[p0][1].append(p1)
                    G1[p1][1].append(p0)
                dx2, dy2 = dd[k-3]
                if S[i+dy2][j+dx2] == "#":
                    p0 = (i*W + j)*4 + (k+1)%4
                    p1 = (y*W + x)*4 + k
                    G0[p0][0].append(p1)
                    G1[p1][0].append(p0)
    used = set()
    que = deque()
    for k in range(4):
        if k != ks:
            s = ((sy1*W + sx1)*4 + ks, (sy*W + sx)*4 + k)
            que.append(s)
            used.add(s)
    while que:
        v1, v2 = que.popleft()
        for k in range(2):
            for w1 in G0[v1][k]:
                for w2 in G1[v2][k^1]:
                    s = (w1, w2)
                    if s in used:
                        continue
                    que.append(s)
                    used.add(s)
    ok = 0
    for k in range(4):
        if k != kg:
            s = ((gy*W + gx)*4 + k, (gy1*W + gx1)*4 + kg)
            if s in used:
                ok = 1
    if ok:
        write("He can accomplish his mission.\n")
        return True

    que = deque([(sy1*W + sx1)*4 + ks])
    used = [0]*N
    used[(sy1*W + sx1)*4 + ks] = 1
    while que:
        v = que.popleft()
        for w in G0[v][0]:
            if used[w]:
                continue
            used[w] = 1
            que.append(w)
        for w in G0[v][1]:
            if used[w]:
                continue
            used[w] = 1
            que.append(w)
    for k in range(4):
        if used[(gy*W + gx)*4 + k]:
            ok = 1
    if ok:
        write("He cannot return to the kitchen.\n")
    else:
        write("He cannot bring tea to his master.\n")
    return True
while solve():
    ...

```