BFS
using namespace std;
int i, j, k;
char map[31][31][31];
int dir[6][3]=
{
{1,0,0},{0,1,0},{0,0,1},
{-1,0,0},{0,-1,0},{0,0,-1}
};
int si, sj, sk;
int L, R, C;
struct node {
int x, y, z, step;
}b;
bool check(int x, int y, int z)
{
if (x > 0 && x <= L && y > 0 && y <= R && z > 0 && z <= C)
return true;
else return false;
}
void input()
{
getchar();
for (i = 1; i <= L; ++i)
{
for (j = 1; j <= R; ++j)
{
for (k = 1; k <= C; ++k)
{
scanf("%c", &map[i][j][k]);
if (map[i][j][k] == 'S')
{
b.x = i; b.y = j; b.z = k;
b.step = 0;
}
}
getchar();
}
getchar();
}
}
void bfs(node a)
{
queue<node>q;
node now, next;
now.x = a.x; now.y = a.y; now.z = a.z;
q.push(a);
while (!q.empty())
{
now = q.front();
q.pop();
for (i = 0; i < 6; ++i)
{
next.x = now.x + dir[i][0];
next.y = now.y + dir[i][1];
next.z = now.z + dir[i][2];
next.step = now.step;
if (check(next.x, next.y, next.z) && (map[next.x][next.y][next.z] == '.'||map[next.x][next.y][next.z]=='E'))
{
++next.step;
if (map[next.x][next.y][next.z]=='E')
{
/*if (next.step == 1)printf("Escaped in 1 minute.");
else*/ printf("Escaped in %d minute(s).", next.step);
printf("\n"); return;
}
map[next.x][next.y][next.z] = '#';
q.push(next);
}
}
}
printf("Trapped!\n");
}
int main()
{
while (scanf("%d%d%d", &L, &R, &C) && L != 0 && R != 0 && C != 0)
{
input();
bfs(b);
}
return 0;
}