#include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)
typedef pair< int, int > pii;

// L,U,R,D --- right:+1, left:-1
const int dx[4] = {-1,0,1,0};
const int dy[4] = {0,1,0,-1};
const int dc[2] = {-1,1};

int changeDirection(int f, int c) { return (f + dc[c] + 4) % 4; }

int W, H;
string home_map[16];

pii nextPos(int cx, int cy, int d) {
	while (home_map[cy + dy[d]][cx + dx[d]] != '#') {
		cx += dx[d];
		cy += dy[d];
	}
	return pii(cx, cy);
}

/***************** solver ********************/

int trans[1024][4][2];
vector< int > trans_rev[1024][4][2];
int start_id, start_dir, goal_id, goal_dir;
vector< int > wall_dir_at_M, wall_dir_at_K;

void constructTransition() {
	memset(trans, -1, sizeof(trans));
	for_(i,0,1024) for_(j,0,4) for_(k,0,2) trans_rev[i][j][k].clear();
	wall_dir_at_M.clear();
	wall_dir_at_K.clear();
	
	map< pii, int > pos_id;
	int id = 1;
	
	for_(y,0,H) for_(x,0,W) {
		if (home_map[y][x] == 'K') pos_id[pii(x, y)] = 0;
		if (home_map[y][x] == 'M') pos_id[pii(x, y)] = 1023;
		if (home_map[y][x] == '.') pos_id[pii(x, y)] = id++;
	}
	
	for_(y,0,H) for_(x,0,W) {
		if (home_map[y][x] == 'M' || home_map[y][x] == 'K') {
			for_(d,0,4) {
				int nx = x + dx[d], ny = y + dy[d];
				if (home_map[ny][nx] == '#') {
					if (home_map[y][x] == 'M') wall_dir_at_M.push_back(d);
					if (home_map[y][x] == 'K') wall_dir_at_K.push_back(d);
					continue;
				}
				
				if (home_map[y][x] == 'M') {
					goal_id = pos_id[nextPos(nx, ny, d)];
					goal_dir = d;
				} else {
					start_id = pos_id[nextPos(nx, ny, d)];
					start_dir = d;
				}
			}
		}
	}
	
	for_(y,0,H) for_(x,0,W) {
		if (home_map[y][x] == '#') continue;
		id = pos_id[pii(x, y)];
		for_(f,0,4) for_(c,0,2) {
			int nxt_dir = changeDirection(f, c);
			int nxt_id = pos_id[nextPos(x, y, nxt_dir)];
			trans[id][f][c] = nxt_id;
			trans_rev[nxt_id][nxt_dir][c].push_back(id);
		}
	}
}

struct State {
	int id, dir;
	int rev_id, rev_dir;
};

bool vis[1024][4][1024][4];

bool canReachMaster() {
	memset(vis, 0, sizeof(vis));
	
	queue< State > que;
	que.push(State{start_id, start_dir});
	vis[start_id][start_dir][0][0] = true;
	
	while (!que.empty()) {
		State s = que.front(); que.pop();
		for_(c,0,2) {
			int nxt_id = trans[s.id][s.dir][c], nxt_dir = changeDirection(s.dir, c);
			if (!vis[nxt_id][nxt_dir][0][0]) {
				vis[nxt_id][nxt_dir][0][0] = true;
				que.push(State{nxt_id, nxt_dir});
			}
		}
	}
	
	bool flag = false;
	for (int f : wall_dir_at_M) flag |= vis[1023][f][0][0];
	return flag;
}

void bfs() {
	memset(vis, 0, sizeof(vis));
	
	queue< State > que;
	for (int d : wall_dir_at_K) {
		que.push(State{start_id, start_dir, 0, d});
		vis[start_id][start_dir][0][d] = true;
	}
	
	while (!que.empty()) {
		State s = que.front(); que.pop();
		for_(c,0,2) {
			int nxt_id = trans[s.id][s.dir][c], nxt_dir = changeDirection(s.dir, c), nxt_rev_dir = changeDirection(s.rev_dir, c);
			for (int nxt_rev_id : trans_rev[s.rev_id][s.rev_dir][1 - c]) {
				if (!vis[nxt_id][nxt_dir][nxt_rev_id][nxt_rev_dir]) {
					vis[nxt_id][nxt_dir][nxt_rev_id][nxt_rev_dir] = true;
					que.push(State{nxt_id, nxt_dir, nxt_rev_id, nxt_rev_dir});
				}
			}
		}
	}
}

void solve() {
	constructTransition();
	canReachMaster();
	bfs();
	
	bool ans = false;
	for (int f : wall_dir_at_M) ans |= vis[1023][f][goal_id][goal_dir];
	
	if (ans) {
		puts("He can accomplish his mission.");
	} else {
		if (canReachMaster()) puts("He cannot return to the kitchen.");
		else puts("He cannot bring tea to his master.");
	}
}

int main() {
	while (1) {
		cin >> W >> H;
		if (H == 0) break;
		for_(y,0,H) cin >> home_map[y];
		solve();
	}
}
### Prompt

In CPP, your task is to solve the following problem:

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.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=(a);i<(b);++i)
typedef pair< int, int > pii;

// L,U,R,D --- right:+1, left:-1
const int dx[4] = {-1,0,1,0};
const int dy[4] = {0,1,0,-1};
const int dc[2] = {-1,1};

int changeDirection(int f, int c) { return (f + dc[c] + 4) % 4; }

int W, H;
string home_map[16];

pii nextPos(int cx, int cy, int d) {
	while (home_map[cy + dy[d]][cx + dx[d]] != '#') {
		cx += dx[d];
		cy += dy[d];
	}
	return pii(cx, cy);
}

/***************** solver ********************/

int trans[1024][4][2];
vector< int > trans_rev[1024][4][2];
int start_id, start_dir, goal_id, goal_dir;
vector< int > wall_dir_at_M, wall_dir_at_K;

void constructTransition() {
	memset(trans, -1, sizeof(trans));
	for_(i,0,1024) for_(j,0,4) for_(k,0,2) trans_rev[i][j][k].clear();
	wall_dir_at_M.clear();
	wall_dir_at_K.clear();
	
	map< pii, int > pos_id;
	int id = 1;
	
	for_(y,0,H) for_(x,0,W) {
		if (home_map[y][x] == 'K') pos_id[pii(x, y)] = 0;
		if (home_map[y][x] == 'M') pos_id[pii(x, y)] = 1023;
		if (home_map[y][x] == '.') pos_id[pii(x, y)] = id++;
	}
	
	for_(y,0,H) for_(x,0,W) {
		if (home_map[y][x] == 'M' || home_map[y][x] == 'K') {
			for_(d,0,4) {
				int nx = x + dx[d], ny = y + dy[d];
				if (home_map[ny][nx] == '#') {
					if (home_map[y][x] == 'M') wall_dir_at_M.push_back(d);
					if (home_map[y][x] == 'K') wall_dir_at_K.push_back(d);
					continue;
				}
				
				if (home_map[y][x] == 'M') {
					goal_id = pos_id[nextPos(nx, ny, d)];
					goal_dir = d;
				} else {
					start_id = pos_id[nextPos(nx, ny, d)];
					start_dir = d;
				}
			}
		}
	}
	
	for_(y,0,H) for_(x,0,W) {
		if (home_map[y][x] == '#') continue;
		id = pos_id[pii(x, y)];
		for_(f,0,4) for_(c,0,2) {
			int nxt_dir = changeDirection(f, c);
			int nxt_id = pos_id[nextPos(x, y, nxt_dir)];
			trans[id][f][c] = nxt_id;
			trans_rev[nxt_id][nxt_dir][c].push_back(id);
		}
	}
}

struct State {
	int id, dir;
	int rev_id, rev_dir;
};

bool vis[1024][4][1024][4];

bool canReachMaster() {
	memset(vis, 0, sizeof(vis));
	
	queue< State > que;
	que.push(State{start_id, start_dir});
	vis[start_id][start_dir][0][0] = true;
	
	while (!que.empty()) {
		State s = que.front(); que.pop();
		for_(c,0,2) {
			int nxt_id = trans[s.id][s.dir][c], nxt_dir = changeDirection(s.dir, c);
			if (!vis[nxt_id][nxt_dir][0][0]) {
				vis[nxt_id][nxt_dir][0][0] = true;
				que.push(State{nxt_id, nxt_dir});
			}
		}
	}
	
	bool flag = false;
	for (int f : wall_dir_at_M) flag |= vis[1023][f][0][0];
	return flag;
}

void bfs() {
	memset(vis, 0, sizeof(vis));
	
	queue< State > que;
	for (int d : wall_dir_at_K) {
		que.push(State{start_id, start_dir, 0, d});
		vis[start_id][start_dir][0][d] = true;
	}
	
	while (!que.empty()) {
		State s = que.front(); que.pop();
		for_(c,0,2) {
			int nxt_id = trans[s.id][s.dir][c], nxt_dir = changeDirection(s.dir, c), nxt_rev_dir = changeDirection(s.rev_dir, c);
			for (int nxt_rev_id : trans_rev[s.rev_id][s.rev_dir][1 - c]) {
				if (!vis[nxt_id][nxt_dir][nxt_rev_id][nxt_rev_dir]) {
					vis[nxt_id][nxt_dir][nxt_rev_id][nxt_rev_dir] = true;
					que.push(State{nxt_id, nxt_dir, nxt_rev_id, nxt_rev_dir});
				}
			}
		}
	}
}

void solve() {
	constructTransition();
	canReachMaster();
	bfs();
	
	bool ans = false;
	for (int f : wall_dir_at_M) ans |= vis[1023][f][goal_id][goal_dir];
	
	if (ans) {
		puts("He can accomplish his mission.");
	} else {
		if (canReachMaster()) puts("He cannot return to the kitchen.");
		else puts("He cannot bring tea to his master.");
	}
}

int main() {
	while (1) {
		cin >> W >> H;
		if (H == 0) break;
		for_(y,0,H) cin >> home_map[y];
		solve();
	}
}
```