#include iostream
#include queue
using namespace std;
char map[201][201];
bool vis[201][201];
int R, C, sx, sy;
struct Node {
int x, y, step;
};
int bfs(int x, int y) {
queue《Node》 q;
q.push({x, y, 0});
vis[x][y] = true;
while (!q.empty()) {
q.pop();
if (map[u.x][u.y] == 'E') {
}
for (int i = 0; i < 4; i++) {
continue;
continue;
vis[nx][ny] = true;
}
}
}
int main() {
cin >> R >> C;
for (int i = 1; i <= R; i++) {
for (int j = 1; j <= C; j++) {
cin >> map[i][j];
if (map[i][j] == 'S') {
sx = i;
sy = j;
}
}
}
if (s == -1)
cout << -1 << endl;
else
cout << s << endl;
return 0;
}