#include #include using namespace std; using namespace atcoder; using ll = long long; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } ll inf_ll = 9223372036854775807; int inf_int = 2147483647; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) using mint = atcoder::modint998244353; using mint1 = atcoder::modint1000000007; using Pa = std::pair; int Yes(bool x){ if(x) cout << "Yes"; else cout << "No"; cout << endl; return 0; } struct point{ int x; int y; int p; }; int main(){ int H, W, M; cin >> H >> W >> M; vector S(H); int a, b, c, d; rep(i, H){ cin >> S[i]; rep(j, W){ if(S[i][j] == 'S'){ a = i; b = j; } if(S[i][j] == 'G'){ c = i; d = j; } } } vector>> A(H, vector>(W, vector(M+1, inf_int))); queue Q; A[a][b][0] = 0; point x; x.x = a; x.y = b; x.p = 0; Q.push(x); ll dx[4] = {1, -1, 0, 0}; ll dy[4] = {0, 0, 1, -1}; while(!Q.empty()){ auto y = Q.front(); Q.pop(); rep(i, 4){ if(y.x+dx[i]>=0 && y.x+dx[i]=0 && y.y+dy[i]