#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int x,y,z;
};
queue <node> qq;
char map[35][35][35];
int track[35][35][35];
int l,r,c;
int start[1][5];
int end[1][5];
int main()
{
void bfs();
while(scanf("%d%d%d",&l,&r,&c)!=EOF)
{
while(!qq.empty()) //之前没这个导致WA
qq.pop();
getchar();
memset(track,0,sizeof(track));
memset(map,0,sizeof(map));
if (!l && !r && !c) break;
int i,j,k;
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')
{
start[0][1]=i;
start[0][2]=j;
start[0][3]=k;
}
if (map[i][j][k]=='E')
{
end[0][1]=i;
end[0][2]=j;
end[0][3]=k;
}
}
getchar();
}
if (i<l) getchar();
}
node a;
a.z=start[0][1];
a.x=start[0][2];
a.y=start[0][3];
qq.push(a);
bfs();
}
return 0;
}
int legal(int zz, int rr,int cc)
{
int ok=0;
if ( rr>=1 && rr<=r && cc>=1 && cc<=c && zz>=1 && zz<=l ) ok=1;
int ok1=0;
if ( map[zz][rr][cc]=='.'|| map[zz][rr][cc]=='E' ) ok1=1;
if (ok1&&ok )
return 1;
return 0;
}
void bfs()
{
int ok=0;
while ( !qq.empty() )
{
node
point=qq.front();
qq.pop();
if (point.z==end[0][1] && point.x==end[0][2] && point.y==end[0][3] )
{ok=1; break; }
map[point.z][point.x][point.y]='#';
if ( legal(point.z,point.x-1,point.y) )
{
point.x--;
qq.push(point);
map[point.z][point.x][point.y]='#'; //之前没这个导致TLE
point.x++;
track[point.z][point.x-1][point.y]=track[point.z][point.x][point.y]+1;
}
if ( legal(point.z,point.x+1,point.y) )
{
point.x++;
qq.push(point);
map[point.z][point.x][point.y]='#';//之前没这个导致TLE
point.x--;
track[point.z][point.x+1][point.y]=track[point.z][point.x][point.y]+1;
}
if ( legal(point.z,point.x,point.y-1) )
{
point.y--;
qq.push(point);
map[point.z][point.x][point.y]='#';//之前没这个导致TLE
point.y++;
track[point.z][point.x][point.y-1]=track[point.z][point.x][point.y]+1;
}
if ( legal(point.z,point.x,point.y+1) )
{
point.y++;
qq.push(point);
map[point.z][point.x][point.y]='#';
point.y--;
track[point.z][point.x][point.y+1]=track[point.z][point.x][point.y]+1;
}
if ( legal(point.z+1,point.x,point.y) )
{
point.z++;
qq.push(point);
map[point.z][point.x][point.y]='#';
point.z--;
track[point.z+1][point.x][point.y]=track[point.z][point.x][point.y]+1;
}
if ( legal(point.z-1,point.x,point.y) )
{
point.z--;
qq.push(point);
map[point.z][point.x][point.y]='#';
point.z++;
track[point.z-1][point.x][point.y]=track[point.z][point.x][point.y]+1;
}
}
if (ok) printf("Escaped in %d minute(s).\n",track[end[0][1]][end[0][2]][end[0][3]]);
else
printf("Trapped!\n");
return ;
}