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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<P, ll> PPI;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX/3

int h,w,k;
char a[810][810];
int dd[]={1,0,-1,0,1};
int si,sj;
int dist[810][810],dist2[810][810];

bool valid(int i,int j){
  return i>=0&&i<h&&j>=0&&j<w;
}

int manh(int i,int j){
  return abs(i-si)+abs(j-sj);
}

int main(){
	cin.sync_with_stdio(false);
  cin>>h>>w>>k;
  rep(i,h){
    cin>>a[i];
    rep(j,w)if(a[i][j]=='S'){
      si=i; sj=j;
      a[i][j]='.';
    }
  }
  rep(i,h)rep(j,w)dist[i][j]=INF;
  queue<PPI> que;
  que.push(PPI(P(si,sj),0));
  while(que.size()){
    PPI np=que.front(); que.pop();
    int ni=np.fi.fi, nj=np.fi.se;
    if(dist[ni][nj]!=INF)continue;
    dist[ni][nj]=np.se;
    rep(d,4){
      int pi=ni+dd[d],pj=nj+dd[d+1];
      if(valid(pi,pj)&&a[pi][pj]!='#')que.push(PPI(P(pi,pj),np.se+1));
    }
  }
  int mind=INF;
  rep(i,h){
    minch(mind,min(dist[i][0],manh(i,0)+k));
    minch(mind,min(dist[i][w-1],manh(i,w-1)+k));
  }
  rep(i,w){
    minch(mind,min(dist[0][i],manh(0,i)));
    minch(mind,min(dist[h-1][i],manh(h-1,i)));
  }
  //dbg(mind);
  cout<<ceil((double)mind/k)<<endl;
	return 0;
}