당신은 상범 빌딩에 갇히고 말았다. 여기서 탈출하는 가장 빠른 길은 무엇일까? 상범 빌딩은 각 변의 길이가 1인 정육면체(단위 정육면체)로 이루어져있다. 각 정육면체는 금으로 이루어져 있어 지나갈 수 없거나, 비어있어서 지나갈 수 있게 되어있다. 당신은 각 칸에서 인접한 6개의 칸(동,서,남,북,상,하)으로 1분의 시간을 들여 이동할 수 있다. 즉, 대각선으로 이동하는 것은 불가능하다. 그리고 상범 빌딩의 바깥면도 모두 금으로 막혀있어 출구를 통해서만 탈출할 수 있다.
당신은 상범 빌딩을 탈출할 수 있을까? 만약 그렇다면 얼마나 걸릴까?
# 6593
import sys
input = lambda: sys.stdin.readline().strip()
# 1. 각 칸에서 인접한 6개의 칸(동, 서, 남, 북, 상, 하)으로 1분의 시간을 들여 이동한다
# 2. 지나갈 수 없는 칸은 '#', 비어있는 칸 '.', 시작 지점 'S', 출구 'E'
from collections import deque
l, r, c = map(int, input().split())
arr, visited = [], []
dx, dy, dz = [1, -1, 0, 0, 0, 0], [0, 0, -1, 0, 1, 0], [0, 0, 0, 1, 0, -1]
def bfs(x, y, z, time = 0):
global l, r, c, arr, dx, dy, dz, visited
queue = deque()
queue.append((x, y, z, time))
visited[x][y][z] = True
while queue:
x, y, z, time = queue.popleft()
if arr[x][y][z] == "E":
return time
for i in range(6):
nx, ny, nz = x + dx[i], y + dy[i], z + dz[i]
if nx < 0 or ny < 0 or nz < 0 or nx >= l or ny >= r or nz >= c:
continue
if visited[nx][ny][nz] or arr[nx][ny][nz] == "#":
continue
visited[nx][ny][nz] = True
queue.append((nx, ny, nz, time+1))
return 0
while l != 0 and r != 0 and c!= 0:
start = 0
arr = []
for x in range(l):
arr.append([])
arr[x] = [list(input()) for _ in range(r)]
not_input = input()
if start == 0:
for y in range(r):
for z in range(c):
if arr[x][y][z] == "S":
start = (x, y, z)
visited = [[[False] * c for _ in range(r)] for _ in range(l)]
x, y, z = start
time = bfs(x, y, z)
if time:
print("Escaped in "+ str(time)+ " minute(s).")
else:
print("Trapped!")
l, r, c = map(int, input().split())