Problem:
Score : 700 points

Problem Statement

Takahashi is locked within a building.

This building consists of H×W rooms, arranged in H rows and W columns.
We will denote the room at the i-th row and j-th column as (i,j). The state of this room is represented by a character A_{i,j}. If A_{i,j}= #, the room is locked and cannot be entered; if A_{i,j}= ., the room is not locked and can be freely entered.
Takahashi is currently at the room where A_{i,j}= S, which can also be freely entered.

Each room in the 1-st row, 1-st column, H-th row or W-th column, has an exit.
Each of the other rooms (i,j) is connected to four rooms: (i-1,j), (i+1,j), (i,j-1) and (i,j+1).

Takahashi will use his magic to get out of the building. In one cast, he can do the following:

Move to an adjacent room at most K times, possibly zero. Here, locked rooms cannot be entered.

Then, select and unlock at most K locked rooms, possibly zero. Those rooms will remain unlocked from then on.

His objective is to reach a room with an exit. Find the minimum necessary number of casts to do so.

It is guaranteed that Takahashi is initially at a room without an exit.

Constraints

3 ≤ H ≤ 800

3 ≤ W ≤ 800

1 ≤ K ≤ H×W

Each A_{i,j} is # , . or S.

There uniquely exists (i,j) such that A_{i,j}= S, and it satisfies 2 ≤ i ≤ H-1 and 2 ≤ j ≤ W-1.

Input

Input is given from Standard Input in the following format:

H W K
A_{1,1}A_{1,2}...A_{1,W}
:
A_{H,1}A_{H,2}...A_{H,W}

Output

Print the minimum necessary number of casts.

Sample Input 1

3 3 3
#.#
#S.
###

Sample Output 1

1

Takahashi can move to room (1,2) in one cast.

Sample Input 2

3 3 3
###
#S#
###

Sample Output 2

2

Takahashi cannot move in the first cast, but can unlock room (1,2).
Then, he can move to room (1,2) in the next cast, achieving the objective in two casts.

Sample Input 3

7 7 2
#######
#######
##...##
###S###
##.#.##
###.###
#######

Sample Output 3

2

Solution in C++11 (GCC 4.8.1) (judge verdict: Accepted)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
vector<string> A;
/*bool search(s,t){
	if(used[s][t] || A[s][t] == '#'){
		return false;
	}
	used[s][t]=true;
	search(s+1,t);
	search(s-1,t);
	search(s,t-1);
	search(s,t+1);
}*/
bool used[805][805];

int main(void){
	int h,w,k;
	cin>>h>>w>>k;
	int memok = k;
	string s;
	rep(i,h){
		cin>>s;
		A.push_back(s);
	}
	rep(i,h){
		rep(j,w){
			used[i][j] = false;
		}
	}
	bool me = true;
	int mi,mj;
	rep(i,h){
		rep(j,w){
			if(A[i][j] == 'S'){
				me = false;
				mi=i;
				mj=j;
			}
			if(!me){
				break;
			}
		}
		if(!me){
			break;
		}
	}
	queue<pair<int, int> > Q;
	pair<int,int> p2;
	p2.first = mi;
	p2.second = mj;
	Q.push(p2);

	while(Q.size() > 0 && k>0){
		queue<pair <int, int> > Q2;
		while(Q.size() > 0){
			pair<int, int> p = Q.front(); Q.pop();
			for(int i=-1;i<=1;i++){
				for(int j=-1;j<=1;j++){
					if(abs(i)+abs(j) != 1) continue;
					if(p.first+i < 0 || p.first+i >= h || p.second+j < 0 || p.second + j >= w) continue;
					if(!used[p.first+i][p.second+j] && A[p.first+i][p.second+j] != '#'){
						pair<int, int> next;
						next.first = p.first+i;
						next.second = p.second+j;
						Q2.push(next);
						used[p.first+i][p.second+j] = true;
					}
				}
			}
			
		}
		Q = Q2;
		k--;
	}
	int ans = min(INF,min(mi,min(h-mi-1,min(mj,w-mj-1))));
	rep(i,h){
		rep(j,w){
			if(used[i][j]){
				ans = min(ans,min(i,min(h-i-1,min(j,w-j-1))));
			}
		}
	}
	cout<<1+(ans/memok) + (ans%memok == 0 ? 0 : 1)<<endl;
	return 0;
}