#include<bits/stdc++.h>

using namespace std;
#define REPP(i,a,b,c) for(int i=a; i<=b; i+=c)
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,c) for(int i=a; i>=b; i-=c)
#define REV(i,a,b) REVV(i,a,b,1)
#define FOR(i,a) REP(i,0,(int)a-1)
#define FORD(i,a) REV(i,(int)a-1,0)
#define PB push_back
#define POB pop_back
#define FI first
#define SE second
#define MP make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef pair < int , int > pii;
typedef vector < int > vi;
typedef vector < pii > vii;
typedef vector < ll > vl;
typedef pair < ll , ll > pll;

const double PI = acos(-1);
const ll MOD = 1e9 + 7;
const double EPS = 1e-9;
const int INF = 1e9;


ll addMod(ll a, ll b){
	if(a + b >= MOD) return a + b - MOD;
	return a + b;
}

ll mulMod(ll a, ll b){
	return (a*b)%MOD;
}

ll fast(ll a, ll b){
	if(b == 0) return 1;
	ll tmp = fast(a,b/2);
	tmp = mulMod(tmp,tmp);
	if(b & 1) tmp = mulMod(tmp,a);
	return tmp;
}

ll GCD(ll a, ll b){
	if(a > b) return GCD(b,a);
	if(a == 0) return b;
	return GCD(b%a,a);
}

//////////////////////////////////////////////

const int N = 800;

int dy[] = {-1,1,0,0};
int dx[] = {0,0,-1,1};
pii st;
int r,c,k;
string M[N + 5];
bool vis[N + 5][N + 5];

bool inRange(int y, int x){
	return y >= 0 && y < r && x >= 0 && x < c;
}

int solve(){
	queue < pair < pii , int > > q;
	while(!q.empty()) q.pop();
	q.push(st);
	int y,x,yy,xx,ret = INF,mn,val,move;
	while(!q.empty()){
		y = q.front().FI.FI;
		x = q.front().FI.SE;
		move = q.front().SE;
		q.pop();
		if(vis[y][x]) continue;
		if(y == 0 || y == r - 1 || x == 0 || x == c-1) return 1;
		vis[y][x] = true;
		mn = min(min(x,c-x-1), min(y, r-y-1));
		val = 0;
		if(mn % k > 0) val = 1;
		val += mn / k;
		ret = min(ret, val+1);
		if(move == 0) continue;
		FOR(i,4){
			yy = y + dy[i];
			xx = x + dx[i];
			if(inRange(yy,xx) && !vis[yy][xx] && M[yy][xx] == '.'){
				q.push(MP(MP(yy,xx),move-1));
			}
		}
	}
	return ret;
}

int main(){
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> r >> c >> k;
	FOR(i,r){
		cin >> M[i];
		FOR(j,c){
			if(M[i][j] == 'S'){
				st = MP(i,j);
			}
		}
	}
	cout << solve() << endl;
	return 0;
}