Problem: There is an evil creature in a square on N-by-M grid (2 \leq N, M \leq 100), and you want to kill it using a laser generator located in a different square. Since the location and direction of the laser generator are fixed, you may need to use several mirrors to reflect laser beams. There are some obstacles on the grid and you have a limited number of mirrors. Please find out whether it is possible to kill the creature, and if possible, find the minimum number of mirrors.

There are two types of single-sided mirrors; type P mirrors can be placed at the angle of 45 or 225 degrees from east-west direction, and type Q mirrors can be placed with at the angle of 135 or 315 degrees. For example, four mirrors are located properly, laser go through like the following.

<image>

Note that mirrors are single-sided, and thus back side (the side with cross in the above picture) is not reflective. You have A type P mirrors, and also A type Q mirrors (0 \leq A \leq 10). Although you cannot put mirrors onto the squares with the creature or the laser generator, laser beam can pass through the square. Evil creature is killed if the laser reaches the square it is in.



Input

Each test case consists of several lines.

The first line contains three integers, N, M, and A. Each of the following N lines contains M characters, and represents the grid information. '#', '.', 'S', 'G' indicates obstacle, empty square, the location of the laser generator, and the location of the evil creature, respectively. The first line shows the information in northernmost squares and the last line shows the information in southernmost squares. You can assume that there is exactly one laser generator and exactly one creature, and the laser generator emits laser beam always toward the south.

Output

Output the minimum number of mirrors used if you can kill creature, or -1 otherwise.

Examples

Input

3 3 2
S#.
...
.#G


Output

2


Input

3 3 1
S#.
...
.#G


Output

-1


Input

3 3 1
S#G
...
.#.


Output

2


Input

4 3 2
S..
...
..#
.#G


Output

-1

Solution (in CPP):

Solution:
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;
  
static const int tx[] = {+0,+1,+0,-1};
static const int ty[] = {-1,+0,+1,+0};

class State{
public:
  int x;
  int y;
  int prev_dir;
  int P_reflect_count; //2->1, 1->2, 0->3, 3->0
  int Q_reflect_count;
  int life;
  map<int,bool> P_mirror;//2->3, 3->2, 1->0, 0->1
  map<int,bool> Q_mirror;//2->3, 3->2, 1->0, 0->1
  State() : x(0),y(0),P_reflect_count(0),Q_reflect_count(0),prev_dir(-1),life(0) {}
  State(int _x,int _y,int _p,int _q,int _d,
	map<int,bool>& _P_mirror,map<int,bool>& _Q_mirror,int _l) : x(_x),y(_y),P_reflect_count(_p),Q_reflect_count(_q),prev_dir(_d),life(_l) {
    P_mirror = _P_mirror;
    Q_mirror = _Q_mirror;
  }

  State(int _x,int _y,int _p,int _q,int _d) : x(_x),y(_y),P_reflect_count(_p),Q_reflect_count(_q),prev_dir(_d),life(0) { }
  bool operator<(const State& s) const{
    return ((P_reflect_count + Q_reflect_count)
	    < (s.P_reflect_count + s.Q_reflect_count));
  }
  bool operator>(const State& s) const{
    return ((P_reflect_count + Q_reflect_count)
	    > (s.P_reflect_count + s.Q_reflect_count));
  }
};

bool check(int x,int y,int gx,int gy,int dir,
	   map<int,bool>& P_mirror,map<int,bool>& Q_mirror,
	   int life,
	   char stage[100][100],int W,int H){
  if(x == gx && y == gy && life == 0) return true;

  bool res = false;
  int dx = x + tx[dir];
  int dy = y + ty[dir];
  if(dx < 0 || dy < 0 || dx >= W || dy >= H) return false;
  if(stage[dy][dx] == '#') return false;

  int next_dir = dir;
  if(P_mirror.find(dy*W+dx) != P_mirror.end()){
    if(dir == 0) next_dir = 3;
    if(dir == 1) next_dir = 2;
    if(dir == 2) next_dir = 1;
    if(dir == 3) next_dir = 0;
  }

  if(Q_mirror.find(dy*W+dx) != Q_mirror.end()){
    if(dir == 0) next_dir = 1;
    if(dir == 1) next_dir = 0;
    if(dir == 2) next_dir = 3;
    if(dir == 3) next_dir = 2;
  }

  res |= check(dx,dy,gx,gy,next_dir,P_mirror,Q_mirror,life-1,stage,W,H);
     
  return res;
}

int main(){
  int H,W,A;
  while(~scanf("%d %d %d",&H,&W,&A)){
    char stage[100][100];

    int sx,sy;
    int gx,gy;
    for(int y=0;y<H;y++){
      char line[101];
      scanf("%s",line);
      for(int x=0;x<W;x++){
	stage[y][x] = line[x];
	if(line[x] == 'S'){
	  sx = x;
	  sy = y;
	}
	if(line[x] == 'G'){
	  gx = x;
	  gy = y;
	}
      }
    }

    priority_queue<State,vector<State>,greater<State> > que;
    que.push(State(sx,sy,0,0,4));

    //P 2->1, 1->2, 0->3, 3->0
    //Q 2->3, 3->2, 1->0, 0->1
    int P_bit1=0; P_bit1 |= (1<<1); P_bit1 |= (1<<2);
    int P_bit2=0; P_bit2 |= (1<<0); P_bit2 |= (1<<3);
    int Q_bit1=0; Q_bit1 |= (1<<2); Q_bit1 |= (1<<3);
    int Q_bit2=0; Q_bit2 |= (1<<0); Q_bit2 |= (1<<1);

    bool dp[100][100][10];
    memset(dp,false,sizeof(dp));
    
    int use_mirrors = INF;
    while(!que.empty()){
      State s = que.top();
      que.pop();

      if(dp[s.x][s.y][s.prev_dir]) continue;
      dp[s.x][s.y][s.prev_dir] = true;

      // printf("x:%d y:%d prev:%d\n",s.x,s.y,s.prev_dir);
      if(s.x == gx && s.y == gy){
	if(s.P_reflect_count > A || s.Q_reflect_count > A) continue;
	if(!check(s.x,s.y,sx,sy,(s.prev_dir+2) % 4,
		  s.P_mirror,s.Q_mirror,s.life,stage,W,H)) continue;

	use_mirrors = s.P_reflect_count + s.Q_reflect_count;
	break;
      }

      for(int i=0;i<4;i++){
	if(s.prev_dir == 4 && i != 2) continue;
	int dx = s.x + tx[i];
	int dy = s.y + ty[i];

	if(dx < 0 || dy < 0 || dx >= W || dy >= H) continue;
	if(stage[dy][dx] == '#') continue;

	if(stage[s.y][s.x] == 'S'){
	  if(s.prev_dir != 4 && s.prev_dir != i){
	    continue;
	  }
	}

	//P 2->1, 1->2, 0->3, 3->0
	//Q 2->3, 3->2, 1->0, 0->1
	int dir_history = 0;
	dir_history |= (1<<i);
	if(s.prev_dir != -1){
	  dir_history |= (1<<s.prev_dir);
	}
	int next_P = s.P_reflect_count;
	int next_Q = s.Q_reflect_count;

	map<int,bool> P_mirror = s.P_mirror;
	map<int,bool> Q_mirror = s.Q_mirror;

	if(P_bit1 == dir_history
	   || P_bit2 == dir_history){
	  next_P++;
	  P_mirror[s.y*W+s.x] = true;
	}
	if(Q_bit1 == dir_history
	   || Q_bit2 == dir_history){
	  next_Q++;
	  Q_mirror[s.y*W+s.x] = true;
	}
	if(next_P > A || next_Q > A) continue;
	if(s.prev_dir == (i + 2) % 4) continue;
	que.push(State(dx,dy,next_P,next_Q,i,P_mirror,Q_mirror,s.life+1));
      }
    }
    printf("%d\n",use_mirrors >= INF ? -1 : use_mirrors);
  }
}