Program Problem4;

Type
   map=array[1..5] of string[10];

var
   WGI,MCO:text;
   BigLoop,Loop,Uber,xpos,ypos,counter,blah:integer;
   Thing:Map;
   temp:string;

Procedure InitArray(var Thing:Map);
   var
   priL:integer;

   Begin
   For priL:=1 to 5 do
      Thing[pril]:='';
   End;

Procedure FindS(Thing:map; var Xpos,Ypos:integer);
   var
      priL,secL:integer;
   Begin
      For priL:=1 to 5 do
         Begin
         For secL:=1 to 10 do
            Begin
            If thing[priL,secL]='S' then
               Begin
               Xpos:=secl;
               Ypos:=pril;
               End;
            End;
         End;
    End;

Procedure OhMy(count,xpos,ypos:integer; var Thing:Map);

   Begin

      If (Thing[xpos,ypos]='E') and (count<uber) then
         Begin
         Uber:=Count;
         end
      else
         Begin

         Thing[xpos,ypos]:=' ';

      If ((Thing[xpos-1,ypos]=' ') and (Thing[xpos-1,ypos]<>'x')) and (((xpos-1>0) and (xpos-1<11)) and ((ypos>0) and (ypos<6))) then
         OhMy(count+1,xpos-1,ypos,thing);

      If ((Thing[xpos-1,ypos-1]=' ') and (Thing[xpos-1,ypos-1]<>'x')) and (((xpos-1>0) and (xpos-1<11)) and ((ypos-1>0) and (ypos-1<6))) then
         OhMy(count+1,xpos-1,ypos-1,thing);

      If ((Thing[xpos-1,ypos+1]=' ') and (Thing[xpos-1,ypos+1]<>'x')) and (((xpos-1>0) and (xpos-1<11)) and ((ypos+1>0) and (ypos+1<6))) then
         OhMy(count+1,xpos-1,ypos+1,thing);

      If ((Thing[xpos,ypos+1]=' ') and (Thing[xpos,ypos+1]<>'x')) and (((xpos>0) and (xpos<11)) and ((ypos+1>0) and (ypos+1<6))) then
         OhMy(count+1,xpos,ypos+1,Thing);

      If ((Thing[xpos,ypos-1]=' ') and (Thing[xpos,ypos]<>'x')) and (((xpos>0) and (xpos<11)) and ((ypos-1>0) and (ypos-1<6))) then
         OhMy(count+1,xpos,ypos-1,Thing);

      If ((Thing[xpos+1,ypos]=' ') and (Thing[xpos+1,ypos]<>'x')) and (((xpos+1>0) and (xpos+1<11)) and ((ypos>0) and (ypos<6))) then
         OhMy(count+1,xpos+1,ypos,Thing);

      If ((Thing[xpos+1,ypos+1]=' ') and (Thing[xpos+1,ypos+1]<>'x')) and (((xpos+1>0) and (xpos+1<11)) and ((ypos+1>0) and (ypos+1<6))) then
         OhMy(count+1,xpos+1,ypos+1,Thing);

      If ((Thing[xpos+1,ypos-1]=' ') and (Thing[xpos+1,ypos-1]<>'x')) and (((xpos+1>0) and (xpos+1<11)) and ((ypos-1>0) and (ypos-1<6))) then
         OhMy(count+1,xpos+1,ypos-1,Thing);

         End;

      End;

Begin
   {$I-}Assign(WGI,'Data4.txt');{$I+}
   Reset(WGI);

   {$I-}Assign(MCO,'Out4.txt'){$I+};
   Rewrite(MCO);
   For BigLoop:=1 to 2 do
      Begin
      InitArray(Thing);
      Counter:=0;
      Repeat
         Begin
         counter:=counter+1;
         Readln(WGI,temp);
         If Temp[1]<>'x' then
            Thing[counter]:=temp;
         End;
      Until temp[1]='x';
      Uber:=100;
      FindS(thing,xpos,ypos);
      Ohmy(1,xpos,ypos,Thing);
      {If Uber=100 then
         Uber:=0;}
      Writeln(MCO,Uber);
      End;

   Close(MCO);
End.

