#include <iostream>
using namespace std;
struct node{
int x,y,z;
node *prev;
node (int x0, int y0, int z0, node* las) { x = x0; y=y0; z=z0; prev = las;}
node();
}; //Структура "Узел" используемая в структуре "Очередь"
struct quene{ //Структура "Очередь"
int k=0;
node *last=NULL;
node *first=NULL;
node *tmp=NULL;
void push( int n, int m, int g){
node* x = new node(n,m,g, NULL);
k++;
if(k!=1)
last->prev = x;
last = x;
if(k==1)
first=x;
}
void pop(){
tmp=first;
first=first->prev;
delete tmp;
k--;
}
};
int main()
{
int l,r,c;
cin >> l >> r >> c;
while((l!=0)&&(r!=0)&&(c!=0)){ //Внешний цикл
int mas[l+2][r+2][c+2];
bool masb[l+2][r+2][c+2];
char u;
int st1,st2,st3;
int fs1,fs2,fs3;
for(int i1=0; i1<l+2; i1++){ //Начало считывания и обработки данных
for(int i2=0; i2<r+2; i2++){
for(int i3=0; i3<c+2; i3++){
masb[i1][i2][i3]=0;
if((i1==0)||(i2==0)||(i3==0)||(i1==l+1)||(i2==r+1)||(i3==c+1))
mas[i1][i2][i3]=-1;
else{
cin>>u;
if(u=='#'){
mas[i1][i2][i3]=-1;}
if(u=='.'){
mas[i1][i2][i3]=0;
}
if(u=='S'){
mas[i1][i2][i3]=0;
st1=i1;st2=i2;st3=i3;
}
if(u=='E'){
mas[i1][i2][i3]=0;
fs1=i1;fs2=i2;fs3=i3;
}
}
}
}
} // Конец обработки
quene ts; //Создание очереди
ts.push(st1,st2,st3);// Кладем в нее "стартовую" вершину
while(ts.k!=0){ //Запускаем поиск
int x1=ts.first->x;
int y1=ts.first->y;
int z1=ts.first->z; //Для удобства сохраняем координаты
//обрабатываемой вершины в специальных переменных
if(mas[x1+1][y1][z1]>=0){ // Вершина справа
if(mas[x1+1][y1][z1]!=0)
mas[x1+1][y1][z1]=min(mas[x1+1][y1][z1],mas[x1][y1][z1]+1);
else
mas[x1+1][y1][z1]=mas[x1][y1][z1]+1;
if(masb[x1+1][y1][z1]==0){
masb[x1+1][y1][z1]=1;
ts.push(x1+1,y1,z1);
}
}
if(mas[x1-1][y1][z1]>=0){ //Вершина слева
if(mas[x1-1][y1][z1]!=0)
mas[x1-1][y1][z1]=min(mas[x1-1][y1][z1],mas[x1][y1][z1]+1);
else
mas[x1-1][y1][z1]=mas[x1][y1][z1]+1;
if(masb[x1-1][y1][z1]==0){
masb[x1-1][y1][z1]=1;
ts.push(x1-1,y1,z1);
}
}
if(mas[x1][y1+1][z1]>=0){ //Вершина вверху
if(mas[x1][y1+1][z1]!=0)
mas[x1][y1+1][z1]=min(mas[x1][y1+1][z1],mas[x1][y1][z1]+1);
else
mas[x1][y1+1][z1]=mas[x1][y1][z1]+1;
if(masb[x1][y1+1][z1]==0){
masb[x1][y1+1][z1]=1;
ts.push(x1,y1+1,z1);
}
}
if(mas[x1][y1-1][z1]>=0){ //Вершина внизу
if(mas[x1][y1-1][z1]!=0)
mas[x1][y1-1][z1]=min(mas[x1][y1-1][z1],mas[x1][y1][z1]+1);
else
mas[x1][y1-1][z1]=mas[x1][y1][z1]+1;
if(masb[x1][y1-1][z1]==0){
masb[x1][y1-1][z1]=1;
ts.push(x1,y1-1,z1);
}
}
if(mas[x1][y1][z1+1]>=0){ //Вершина на уровень выше
if(mas[x1][y1][z1+1]!=0)
mas[x1][y1][z1+1]=min(mas[x1][y1][z1+1],mas[x1][y1][z1]+1);
else
mas[x1][y1][z1+1]=mas[x1][y1][z1]+1;
if(masb[x1][y1][z1+1]==0){
masb[x1][y1][z1+1]=1;
ts.push(x1,y1,z1+1);
}
}
if(mas[x1][y1][z1-1]>=0){ //Вершина на уровень ниже
if(mas[x1][y1][z1-1]!=0)
mas[x1][y1][z1-1]=min(mas[x1][y1][z1-1],mas[x1][y1][z1]+1);
else
mas[x1][y1][z1-1]=mas[x1][y1][z1]+1;
if(masb[x1][y1][z1-1]==0){
masb[x1][y1][z1-1]=1;
ts.push(x1,y1,z1-1);
}
}
ts.pop(); //Удаление обработанной вершины из очереди
}
if(mas[fs1][fs2][fs3]!=0) // Последняя проверка результата
cout <<"Escaped in " << mas[fs1][fs2][fs3] << " minute(s)."<< endl;
else
cout << "Trapped!" << endl;
cin >> l >> r >> c; // Считываение чисел для внешнего цикла
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBub2RlewogICBpbnQgeCx5LHo7CiAgIG5vZGUgKnByZXY7CiAgIG5vZGUgKGludCB4MCwgaW50IHkwLCBpbnQgejAsICBub2RlKiBsYXMpIHsgeCA9IHgwOyB5PXkwOyB6PXowOyAgcHJldiA9IGxhczt9CiAgIG5vZGUoKTsKfTsgIC8v0KHRgtGA0YPQutGC0YPRgNCwICLQo9C30LXQuyIg0LjRgdC/0L7Qu9GM0LfRg9C10LzQsNGPINCyINGB0YLRgNGD0LrRgtGD0YDQtSAi0J7Rh9C10YDQtdC00YwiCgpzdHJ1Y3QgcXVlbmV7IC8v0KHRgtGA0YPQutGC0YPRgNCwICLQntGH0LXRgNC10LTRjCIKICAgaW50IGs9MDsKICAgbm9kZSAqbGFzdD1OVUxMOwogICBub2RlICpmaXJzdD1OVUxMOwogICBub2RlICp0bXA9TlVMTDsKCiAgIHZvaWQgcHVzaCggaW50IG4sIGludCBtLCBpbnQgZyl7CiAgICAgICBub2RlKiB4ID0gbmV3IG5vZGUobixtLGcsIE5VTEwpOwogICAgICAgaysrOwogICAgICAgaWYoayE9MSkKICAgICAgICAgICBsYXN0LT5wcmV2ID0geDsKICAgICAgIGxhc3QgPSB4OwogICAgICAgaWYoaz09MSkKICAgICAgICAgICBmaXJzdD14OwogICB9CiAgIAogICB2b2lkIHBvcCgpewogICAgICAgdG1wPWZpcnN0OwogICAgICAgZmlyc3Q9Zmlyc3QtPnByZXY7CiAgICAgICBkZWxldGUgdG1wOwogICAgICAgay0tOwogICAgICAgfQoKfTsKCgoKaW50IG1haW4oKQp7CiAgIGludCBsLHIsYzsKICAgY2luID4+IGwgPj4gciA+PiBjOwogICB3aGlsZSgobCE9MCkmJihyIT0wKSYmKGMhPTApKXsgLy/QktC90LXRiNC90LjQuSDRhtC40LrQuwogICBpbnQgbWFzW2wrMl1bcisyXVtjKzJdOwogICBib29sIG1hc2JbbCsyXVtyKzJdW2MrMl07CiAgIGNoYXIgdTsKICAgaW50IHN0MSxzdDIsc3QzOwogICBpbnQgZnMxLGZzMixmczM7CiAgIGZvcihpbnQgaTE9MDsgaTE8bCsyOyBpMSsrKXsgLy/QndCw0YfQsNC70L4g0YHRh9C40YLRi9Cy0LDQvdC40Y8g0Lgg0L7QsdGA0LDQsdC+0YLQutC4INC00LDQvdC90YvRhQogICAgICAgZm9yKGludCBpMj0wOyBpMjxyKzI7IGkyKyspewogICAgICAgICAgIGZvcihpbnQgaTM9MDsgaTM8YysyOyBpMysrKXsKICAgICAgICAgICAgICAgbWFzYltpMV1baTJdW2kzXT0wOwogICAgICAgICAgICAgICBpZigoaTE9PTApfHwoaTI9PTApfHwoaTM9PTApfHwoaTE9PWwrMSl8fChpMj09cisxKXx8KGkzPT1jKzEpKQogICAgICAgICAgICAgICAgICAgbWFzW2kxXVtpMl1baTNdPS0xOwogICAgICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICBjaW4+PnU7CiAgICAgICAgICAgICAgIGlmKHU9PScjJyl7CiAgICAgICAgICAgICAgIG1hc1tpMV1baTJdW2kzXT0tMTt9CiAgICAgICAgICAgICAgIGlmKHU9PScuJyl7CiAgICAgICAgICAgICAgICAgICBtYXNbaTFdW2kyXVtpM109MDsKICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICBpZih1PT0nUycpewogICAgICAgICAgICAgICAgICAgbWFzW2kxXVtpMl1baTNdPTA7CiAgICAgICAgICAgICAgICAgICBzdDE9aTE7c3QyPWkyO3N0Mz1pMzsKICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICBpZih1PT0nRScpewogICAgICAgICAgICAgICAgICAgbWFzW2kxXVtpMl1baTNdPTA7CiAgICAgICAgICAgICAgICAgICBmczE9aTE7ZnMyPWkyO2ZzMz1pMzsKICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICB9CiAgICAgICAgICAgfQogICAgICAgICAgIH0KICAgICAgIH0gLy8g0JrQvtC90LXRhiDQvtCx0YDQsNCx0L7RgtC60LgKCiAgIHF1ZW5lIHRzOyAvL9Ch0L7Qt9C00LDQvdC40LUg0L7Rh9C10YDQtdC00LgKICAgdHMucHVzaChzdDEsc3QyLHN0Myk7Ly8g0JrQu9Cw0LTQtdC8INCyINC90LXQtSAi0YHRgtCw0YDRgtC+0LLRg9GOIiDQstC10YDRiNC40L3RgwogICB3aGlsZSh0cy5rIT0wKXsgIC8v0JfQsNC/0YPRgdC60LDQtdC8INC/0L7QuNGB0LoKICAgICAgIGludCB4MT10cy5maXJzdC0+eDsKICAgICAgIGludCB5MT10cy5maXJzdC0+eTsKICAgICAgIGludCB6MT10cy5maXJzdC0+ejsgIC8v0JTQu9GPINGD0LTQvtCx0YHRgtCy0LAg0YHQvtGF0YDQsNC90Y/QtdC8INC60L7QvtGA0LTQuNC90LDRgtGLIAogICAgICAgLy/QvtCx0YDQsNCx0LDRgtGL0LLQsNC10LzQvtC5INCy0LXRgNGI0LjQvdGLINCyINGB0L/QtdGG0LjQsNC70YzQvdGL0YUg0L/QtdGA0LXQvNC10L3QvdGL0YUKCiAgICAgICBpZihtYXNbeDErMV1beTFdW3oxXT49MCl7IC8vINCS0LXRgNGI0LjQvdCwINGB0L/RgNCw0LLQsAogICAgICAgICAgIGlmKG1hc1t4MSsxXVt5MV1bejFdIT0wKQogICAgICAgICAgICAgICBtYXNbeDErMV1beTFdW3oxXT1taW4obWFzW3gxKzFdW3kxXVt6MV0sbWFzW3gxXVt5MV1bejFdKzEpOwogICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgbWFzW3gxKzFdW3kxXVt6MV09bWFzW3gxXVt5MV1bejFdKzE7CiAgICAgICAgICAgaWYobWFzYlt4MSsxXVt5MV1bejFdPT0wKXsKICAgICAgICAgICAgICAgbWFzYlt4MSsxXVt5MV1bejFdPTE7CiAgICAgICAgICAgICAgIHRzLnB1c2goeDErMSx5MSx6MSk7CiAgICAgICAgICAgfQogICAgICAgfQoKICAgICAgIGlmKG1hc1t4MS0xXVt5MV1bejFdPj0wKXsgLy/QktC10YDRiNC40L3QsCDRgdC70LXQstCwCiAgICAgICAgICAgaWYobWFzW3gxLTFdW3kxXVt6MV0hPTApCiAgICAgICAgICAgICAgIG1hc1t4MS0xXVt5MV1bejFdPW1pbihtYXNbeDEtMV1beTFdW3oxXSxtYXNbeDFdW3kxXVt6MV0rMSk7CiAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICBtYXNbeDEtMV1beTFdW3oxXT1tYXNbeDFdW3kxXVt6MV0rMTsKICAgICAgICAgICBpZihtYXNiW3gxLTFdW3kxXVt6MV09PTApewogICAgICAgICAgICAgICBtYXNiW3gxLTFdW3kxXVt6MV09MTsKICAgICAgICAgICAgICAgdHMucHVzaCh4MS0xLHkxLHoxKTsKICAgICAgICAgICB9CiAgICAgICB9CgogICAgICAgaWYobWFzW3gxXVt5MSsxXVt6MV0+PTApeyAvL9CS0LXRgNGI0LjQvdCwINCy0LLQtdGA0YXRgwogICAgICAgICAgIGlmKG1hc1t4MV1beTErMV1bejFdIT0wKQogICAgICAgICAgICAgICBtYXNbeDFdW3kxKzFdW3oxXT1taW4obWFzW3gxXVt5MSsxXVt6MV0sbWFzW3gxXVt5MV1bejFdKzEpOwogICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgbWFzW3gxXVt5MSsxXVt6MV09bWFzW3gxXVt5MV1bejFdKzE7CiAgICAgICAgICAgaWYobWFzYlt4MV1beTErMV1bejFdPT0wKXsKICAgICAgICAgICAgICAgbWFzYlt4MV1beTErMV1bejFdPTE7CiAgICAgICAgICAgICAgIHRzLnB1c2goeDEseTErMSx6MSk7CiAgICAgICAgICAgfQogICAgICAgfQoKICAgICAgIGlmKG1hc1t4MV1beTEtMV1bejFdPj0wKXsgLy/QktC10YDRiNC40L3QsCDQstC90LjQt9GDCiAgICAgICAgICAgaWYobWFzW3gxXVt5MS0xXVt6MV0hPTApCiAgICAgICAgICAgICAgIG1hc1t4MV1beTEtMV1bejFdPW1pbihtYXNbeDFdW3kxLTFdW3oxXSxtYXNbeDFdW3kxXVt6MV0rMSk7CiAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICBtYXNbeDFdW3kxLTFdW3oxXT1tYXNbeDFdW3kxXVt6MV0rMTsKICAgICAgICAgICBpZihtYXNiW3gxXVt5MS0xXVt6MV09PTApewogICAgICAgICAgICAgICBtYXNiW3gxXVt5MS0xXVt6MV09MTsKICAgICAgICAgICAgICAgdHMucHVzaCh4MSx5MS0xLHoxKTsKICAgICAgICAgICB9CiAgICAgICB9CgogICAgICAgaWYobWFzW3gxXVt5MV1bejErMV0+PTApeyAvL9CS0LXRgNGI0LjQvdCwINC90LAg0YPRgNC+0LLQtdC90Ywg0LLRi9GI0LUKICAgICAgICAgICBpZihtYXNbeDFdW3kxXVt6MSsxXSE9MCkKICAgICAgICAgICAgICAgbWFzW3gxXVt5MV1bejErMV09bWluKG1hc1t4MV1beTFdW3oxKzFdLG1hc1t4MV1beTFdW3oxXSsxKTsKICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgIG1hc1t4MV1beTFdW3oxKzFdPW1hc1t4MV1beTFdW3oxXSsxOwogICAgICAgICAgIGlmKG1hc2JbeDFdW3kxXVt6MSsxXT09MCl7CiAgICAgICAgICAgICAgIG1hc2JbeDFdW3kxXVt6MSsxXT0xOwogICAgICAgICAgICAgICB0cy5wdXNoKHgxLHkxLHoxKzEpOwogICAgICAgICAgIH0KICAgICAgIH0KCiAgICAgICBpZihtYXNbeDFdW3kxXVt6MS0xXT49MCl7IC8v0JLQtdGA0YjQuNC90LAg0L3QsCDRg9GA0L7QstC10L3RjCDQvdC40LbQtQogICAgICAgICAgIGlmKG1hc1t4MV1beTFdW3oxLTFdIT0wKQogICAgICAgICAgICAgICBtYXNb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