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: Accepted)

/*****************
 *   I<3   C++   *
 *  IWANT ANYAC  *
 * ILOVEPROGRAM! *
 *IT'SINTERESTING*
 * ILOVEPROGRAM! *
 *  INCONTESTS   *
 *   GETSCORE    *
 *    ACCODE     *
 *     LETS      *
 *      GO       *
 *Written by:    *
 *    charlieyan *
 *****************/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <locale>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
#include <wctype.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <complex.h>
#include <fenv.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <numeric>
#include <list>
#define LL long long
#define pb push_back
#define pob pop_back
#define mp make_pair
#define FILL(a,v) memset((void*)a,v,sizeof(a))
#define FILL0(a) FILL(a,0)
#define FILL1(a) FILL(a,-1)
#define FILLBIG(a) FILL(a,0x7f)
#define FILLFBIG(a) FILL(a,0x80)
#define ALL(a) (a).begin(),(a).end()
#define Inf ((int)0x7f7f7f7f)
#define INF ((long long)0x7f7f7f7f7f7f7f7f)
#define FInf ((int)0x80808080)
#define FINF ((long long)0x8080808080808080)
#define Mod 1000000007
#define foreach(it,x) for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define FOR(i,x) for(int i=0;i<x;i++)
#define RFOR(i,x) for(int i=x-1;i>=0;i--)
#define FFOR(i,a,b) for(int i=a;i<b;i++)
#define FRFOR(i,a,b) for(int i=a-1;i>=b;i--)
#define SZ(a) ((int)(a).size())
#define FI first
#define SE second
#define OVER(a) return puts(a),0
#define fastio ios::sync_with_stdio(0)
#define filei(f) freopen(f,"r",stdin)
#define fileo(f) freopen(f,"w",stdout)
#define eprintf(...) fprintf(stderr,__VA_ARGS__)
#define MASK(a) (1<<(a))
#define MASKX(x,a) ((x)<<(a))
#define BIT(a,b) ((a>>b)&1)
#define cddx(a) priority_queue<a >
#define cxdd(a) priority_queue<a,vector<a >,greater<a > >
#define deval(a) {cerr<<#a<<" = "<<a<<endl;}
#define dearr(be,en) {cerr<<#be;cerr<<" = {";for(auto i=be;i!=en;i++)cerr<<' '<<*i;cerr<<" }\n";}
#define reg register
#define prtime eprintf("\n\nTime used: %dms\n",clock());
#define PI 3.1415926535897932384626
#define expause return getchar(),getchar(),0;
//Code from Microsoft Visual Studio 2017 by Charlieyan(or zxf) who hates Xuzhen.
//2018年年度最佳骂人词：你个xuzhen！
using namespace std;
typedef pair<int, int> pii;
typedef unsigned long long UINT;
typedef unsigned int uint;
typedef long long INT;
typedef long double ldouble;
typedef pair<INT, INT> PII;
typedef stringstream sst;
typedef istringstream iss;
typedef vector<int> vint;
typedef vector<INT> VINT;
typedef vector<pii> vpii;
typedef vector<PII> VPII;
typedef priority_queue<int> pqi;
typedef priority_queue<INT> PQI;
typedef priority_queue<pii> pqp;
typedef priority_queue<PII> PQP;
typedef priority_queue<int, vint, greater<int> > xqi;
typedef priority_queue<INT, VINT, greater<INT> > XQI;
typedef priority_queue<pii, vpii, greater<pii> > xqp;
typedef priority_queue<PII, VPII, greater<PII> > XQP;
//Orz Shurong Wang he is a DALAO.
//Orz The_27 he is a SHENBEN.
//Orz Chinauser he is a JULAO
//Don't Orz me. I am a JURUO.

struct ini
{
	int x, y;
	int d, l;
	int b;
	ini() {}
	ini(int x, int y, int d, int l, int b) :x(x), y(y), d(d), l(l), b(b) {}
	bool operator<(const ini& in)const
	{
		if (d > in.d)return 1;
		if (d < in.d)return 0;
		if (l > in.l)return 1;
		if (l < in.l)return 0;
		if (b < in.b)return 1;
		if (b > in.b)return 0;
		return 0;
	}
	bool operator>(const ini& in)const
	{
		if (d > in.d)return 0;
		if (d < in.d)return 1;
		if (l > in.l)return 0;
		if (l < in.l)return 1;
		if (b < in.b)return 0;
		if (b > in.b)return 1;
		return 0;
	}
};

int h, w, k;
char c[807][807];
ini dis[807][807];
int myx, myy;//My pos.
int dx[] = { 1,0,-1,0 }, dy[] = { 0,1,0,-1 };

void dij()
{
	priority_queue<ini> pq;
	FILLBIG(dis);
	pq.push(ini(myx, myy, 1, 0, 0));
	dis[myx][myy] = ini(myx, myy, 1, 0, 0);
	while (SZ(pq))
	{
		ini in = pq.top();
		pq.pop();
		for (int i = 0; i < 4; i++)
		{
			int nx = in.x + dx[i], ny = in.y + dy[i];
			if (nx >= 1 && nx <= h && ny >= 1 && ny <= w)
			{
				int nd = in.d, nl = in.l + 1, nb = in.b;
				if (nl == k + 1)
				{
					nb = k;
					nl = 1;
					nd++;
				}
				if (c[nx][ny] == '#')nb--;
				if (nb == -1)
				{
					nb = k - 1;
					nl = 1;
					nd++;
				}
				if (ini(nx, ny, nd, nl, nb) > dis[nx][ny])
				{
					dis[nx][ny] = ini(nx, ny, nd, nl, nb);
					pq.push(ini(nx, ny, nd, nl, nb));
				}
			}
		}
	}
}

int main()
{
	scanf("%d%d%d", &h, &w, &k);
	for (int i = 1; i <= h; i++)
	{
		scanf("%s", c[i] + 1);
		for (int j = 1; j <= w; j++)
		{
			if (c[i][j] == 'S')
			{
				c[i][j] = '.';
				myx = i, myy = j;
			}
		}
	}
	dij();
	int minv = Inf;
	for (int i = 1; i <= h; i++)
	{
		minv = min(minv, dis[i][1].d);
		minv = min(minv, dis[i][w].d);
	}
	for (int i = 1; i <= w; i++)
	{
		minv = min(minv, dis[1][i].d);
		minv = min(minv, dis[h][i].d);
	}
	printf("%d\n", minv);
	return 0;
}