#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;

LL vis[805][805] = {0};
char ch[805][805];
LL n,m,k,ans = INF;
LL dx[4] = {0,0,1,-1};
LL dy[4] = {1,-1,0,0};

struct node{
	LL x,y,stp;
}h,t; queue <node> q;
void bfs(LL x,LL y){
	t.x = x; t.y = y; t.stp = 0; q.push(t);
	while(!q.empty()){
		h = q.front(); q.pop();
		if(h.stp > k) continue;
		vis[h.x][h.y] = 1;
		ans = min(ans,min(min(h.x - 1,h.y - 1),min(n - h.x,m - h.y)));
		for(LL i = 0;i < 4;i ++){
			LL tx = h.x + dx[i],ty = h.y + dy[i];
			if(!tx || !ty || tx > n || ty > m) continue;
			if(vis[tx][ty] || ch[tx][ty] == '#') continue;
			vis[tx][ty] = 1;
			t.x = tx; t.y = ty; t.stp = h.stp + 1;
			q.push(t);
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	LL sx = 0,sy = 0;
	cin >> n >> m >> k;
	for(LL i = 1;i <= n;i ++){
		for(LL j = 1;j <= m;j ++){
			cin >> ch[i][j];
			if(ch[i][j] == 'S'){ sx = i; sy = j; }
		}
	}
	bfs(sx,sy);
	cout << (ans + k - 1) / k + 1 << endl;
	return 0;
}