//代码如下
#include <cstdio>
#include <queue>using namespace std;
#define Max 105
const int INF=10000000;
typedef pair< int, int > P;
char maze[Max][Max];
int sx, sy;
int gx, gy;
int n, m;
int d[Max][Max];
int dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};
int bfs() {
queue<P>que;
P p;
int i, j, nx, ny;
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
d[i][j] = INF;
d[sx][sy] = 0;
que.push(P(sx, sy));
while(que.size()){
p = que.front();
que.pop();
if(p.first == gx && p.second == gy) break;
for(i = 0; i < 4; i++){
nx = p.first+dx[i], ny = p.second+dy[i];
if((nx >= 0) && (nx < n) && (ny >= 0) && (ny < m))
if((d[nx][ny]==INF) && (maze[nx][ny]!='#')){
que.push(P(nx,ny));
d[nx][ny] = d[p.first][p.second]+1;
}
}
}
return d[gx][gy];
}
void solve()
{
int sum;
sum = bfs();
printf("%d\n",sum);
}
int main()
{
int i,j;
scanf("%d%d",&n,&m);
getchar();
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++){
scanf("%c",&maze[i][j]);
if(maze[i][j] == 'S') { sx = i; sy = j;}
if(maze[i][j] == 'G') { gx = i; gy = j;}
}
getchar();
}
solve();
return 0;
}
//因为在main里面多定义了一个n答案就一直错,果然细节的东西还是要多注意注意