#include <iostream>
#include <queue>
using namespace std;

int main()
{
    int a,b;
    cin>>a>>b;
    char mat[a][b];
    queue<int>Q;
    int poseten[a][b];
    int rs,ks,re,ke;
    for(int i=0;i<a;i++)
    {
        for(int j=0;j<b;j++)
        {
            cin>>mat[i][j];
            if(mat[i][j]=='S')
            {
                rs=i;
                ks=j;
            }
            if(mat[i][j]=='E')
            {
                re=i;
                ke=j;
            }
            poseten[i][j]=false;
        }
    }
    int s[a][b];
    int e[a][b];
    for(int i=0;i < a;i++)
    {
        for(int j=0;j<b;j++)
        {
            s[i][j]=-1;
            e[i][j]=-1;
        }
    }

    Q.push(rs);
    Q.push(ks);
    poseten[rs][ks] = true;
    int kolkuS=0;
    Q.push(kolkuS);
        while(!Q.empty() )
        {
           int ci=Q.front();
           Q.pop();
           int cj=Q.front();
           Q.pop();
           int n=Q.front();
           Q.pop();
            if(ci == re and cj == ke) {
                cout << n << endl;
                return 0;
            }
           if(cj+1<b && poseten[ci][cj+1]!=true && mat[ci][cj+1]!='#')
           {
               Q.push(ci);
               Q.push(cj+1);
               Q.push(n+1);
               poseten[ci][cj+1]=true;
           }
           if(cj+1<b && poseten[ci][cj+1]!=true && mat[ci][cj+1]=='#')
           {
               s[ci][cj+1]=n+1;
           }
           if(cj-1>=0 && poseten[ci][cj-1]!=true && mat[ci][cj-1]!='#')
           {
               Q.push(ci);
               Q.push(cj-1);
               Q.push(n+1);
               poseten[ci][cj-1]=true;
           }
           if(cj-1>=0 && poseten[ci][cj-1]!=true && mat[ci][cj-1]=='#')
           {
               s[ci][cj-1]=n+1;
           }
           if(ci+1<a && poseten[ci+1][cj]!=true && mat[ci+1][cj]!='#')
           {
               Q.push(ci+1);
               Q.push(cj);
               Q.push(n+1);
               poseten[ci+1][cj]=true;
           }
           if(ci+1<a && poseten[ci+1][cj]!=true && mat[ci+1][cj]=='#')
           {
               s[ci+1][cj]=n+1;
           }
           if(ci-1>=0 && poseten[ci-1][cj]!=true && mat[ci-1][cj]!='#')
           {
               Q.push(ci-1);
               Q.push(cj);
               Q.push(n+1);
               poseten[ci-1][cj]=true;
           }
           if(ci-1>=0 && poseten[ci-1][cj]!=true && mat[ci-1][cj]=='#')
           {
               s[ci-1][cj]=n+1;
           }
        }
    for(int i =0 ; i < a; i++) {
        for(int j= 0; j < b; j++) {
            poseten[i][j] = false;
        }
    }
    Q.push(re);
    Q.push(ke);
    poseten[re][ke] = true;
    int kolkuE=0;
    Q.push(kolkuE);
        while(!Q.empty() )
        {
           int ci=Q.front();
           Q.pop();
           int cj=Q.front();
           Q.pop();
           int n=Q.front();
           Q.pop();
           if(cj+1<b && poseten[ci][cj+1]!=true && mat[ci][cj+1]!='#')
           {
               Q.push(ci);
               Q.push(cj+1);
               Q.push(n+1);
               poseten[ci][cj+1]=true;
           }
           if(cj+1<b && poseten[ci][cj+1]!=true && mat[ci][cj+1]=='#')
           {
               e[ci][cj+1]=n+1;
           }
           if(cj-1>=0 && poseten[ci][cj-1]!=true && mat[ci][cj-1]!='#')
           {
               Q.push(ci);
               Q.push(cj-1);
               Q.push(n+1);
               poseten[ci][cj-1]=true;
           }
           if(cj-1>=0 && poseten[ci][cj-1]!=true && mat[ci][cj-1]=='#')
           {
               e[ci][cj-1]=n+1;
           }
           if(ci+1<a && poseten[ci+1][cj]!=true && mat[ci+1][cj]!='#')
           {
               Q.push(ci+1);
               Q.push(cj);
               Q.push(n+1);
               poseten[ci+1][cj]=true;
           }
           if(ci+1<a && poseten[ci+1][cj]!=true && mat[ci+1][cj]=='#')
           {
               e[ci+1][cj]=n+1;
           }
           if(ci-1>=0 && poseten[ci-1][cj]!=true && mat[ci-1][cj]!='#')
           {
               Q.push(ci-1);
               Q.push(cj);
               Q.push(n+1);
               poseten[ci-1][cj]=true;
           }
           if(ci-1>=0 && poseten[ci-1][cj]!=true && mat[ci-1][cj]=='#')
           {
               e[ci-1][cj]=n+1;
           }
        }
        int najgolem=-1;
        for(int i=0;i<a;i++)
        {
            for(int j=0;j<b;j++)
            {
                if(s[i][j] != -1 and e[i][j] != -1 and najgolem<s[i][j]+e[i][j])
                {
                    najgolem=s[i][j]+e[i][j];
                }
            }
        }
        cout<<najgolem;
    return 0;
}
