#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <cctype>
using namespace std;



string field[100];
int lines = 0;
int dist[100][11];

int dir[8][2] = { {0,1}, {1,0}, {0,-1}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1} };

struct point
{
       int x, y;
       point(int a, int b)
       {
                 x = a; y = b;
       }
};

void resetDist()
{
     for (int i = 0; i < 100; ++i)     
              for (int j = 0; j < 11; ++j)
                  dist[i][j] = -1;
}

void resetField()
{
     for (int i = 0; i < 100; ++i)
     {
         field[i] = "";
         for (int j = 0; j < 11; ++j)
         {
             dist[i][j] = -1;
         }
     }
}

point findChar(char param)
{
      for (int i = 0; i < lines; ++i)
      {
          for (int j = 0; j < 10; ++j)
          {
              if (field[i][j] == param)
              {
                 return point(i,j);
              }
          }
      }
}

bool inBounds(point p)
{
     return (p.x >= 0 && p.x < lines && p.y >= 0 && p.y < 10);
}

void bfs()
{
     queue <point> q;
     point start = findChar('S');
     q.push(start);
     point cur(0,0);
     point next(0,0);
     dist[start.x][start.y] = 0;
     while (!q.empty())
     {

           cur = q.front();
           q.pop();
           for (int i = 0; i < 8; ++i)
           {
               next.x = cur.x + dir[i][0];
               next.y = cur.y + dir[i][1]; 
               /*
               cout << "qempty:" << q.empty();
               cout << "next:" << next.x << " " << next.y; cin.get();              
               cout << "cur:" << cur.x << " " << cur.y; cin.get();                         */
               if (inBounds(next) && field[next.x][next.y] != ' ' && dist[next.x][next.y] == -1)
               {
                                         
                                              

//                  cout << 
                  dist[next.x][next.y] = dist[cur.x][cur.y] + 1;
                  q.push(point(next.x, next.y));
               }
           }
           /*
           cout << "\n";
           for (int i = 0; i < 5; ++i)
           {
               for (int j = 0; j < 10; ++j)
               {
                   cout << dist[i][j] + 2;
               }
               cout << "\n";
           }
           for (int i = 0; i < 5; ++i)
           {
               for (int j = 0; j < 10; ++j)
               {
                   cout << field[i][j];
               }
               cout << "\n";
           }
           cin.get();
           */
     }     
}


//MAIN PROGRAM BODY
int main()
{         
    //File I/O declarations
    ofstream fout ("OUT4.txt");
    ifstream fin ("DATA4.txt");
    
    
    int i, j, k; 
    int buffer1, buffer2, buffer3, buffer4;   
    int number;
    int factors = 0;
    int ones;
    
    string buffer;
    i = 0;
    int line = 0;    
    while (!fin.eof())
    {     
          getline(fin, buffer,'\n');
          field[(int)floor(i)] = buffer + '\n';          
          ++i;
          if (buffer == "xxxxxxxxxx")
          {
           
              lines = i - 1;
              //cout << "lines:" << lines;
              resetDist();
              bfs();
              point endPoint = findChar('E');
              fout <<  dist[endPoint.x][endPoint.y] << "\n";
              
              for (j = 0; j < i; ++j)
                  cout << field[j];
              resetField();
              i = 0;
          }          
    }
    

    
    /*
    for (j = 0; j < i; ++j)
        cout << field[j];
    cin.get();
*/

                 
    
    return 0;    
}

