#include using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; #define rep(i,n) for(ll i=0;i T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); } template T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); } template inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template ostream &operator<<(ostream &os, const vector &a){ if (a.empty()) return os; os << a.front(); for (auto e : a | views::drop(1)){ os << ' ' << e; } return os; } void dump(auto ...vs){ ((cout << vs << ' '), ...) << endl; } void solve() { ll BH,BW; cin>>BH>>BW; ll H=2*BH-1; ll W=2*BW-1; vector G(H,string(W,'.')); rep(i,BH){ string S; cin>>S; rep(j,BW){ G[2*i][2*j]=S[j]; } } ll si,sj,gi,gj; rep(i,H){ rep(j,W){ if (G[i][j]=='S'){ si=i; sj=j; } if (G[i][j]=='G'){ gi=i; gj=j; } } } vector dxdy={0,1,0,-1,0}; vector> dist(H,vector(W,1e18)); dist[si][sj]=0; queue> qu; qu.emplace(si,sj); while (qu.size()>0){ auto [ci,cj]=qu.front();qu.pop(); rep(d,4){ ll di=dxdy[d]; ll dj=dxdy[d+1]; ll ni,nj; ni=ci+di; nj=cj+dj; if (0<=ni and nisync_with_stdio(0); ll T=1; while (T--){ solve(); } return 0; }