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

long long H, W, K;

long long A[805][805];
long long WORK[805][805];

#define OPEN INT_MAX
#define CLOSED LONG_MAX/3


bool valid(long long h, long long w){
	return (h >= 0) && (h < H) && (w >= 0) && (w < W);
}

void move(long long h, long long w, long long cost){
	if(valid(h, w) == false || A[h][w] == CLOSED || cost > K || A[h][w] <= cost){
		return;
	}
	A[h][w] = cost;
	move(h-1, w, cost+1);
	move(h+1, w, cost+1);
	move(h, w-1, cost+1);
	move(h, w+1, cost+1);
}

void firstmove(){
	for(long long h = 0; h < H; h++){
		for(long long w = 0; w < W; w++){
			if(A[h][w] == 1){
				move(h, w, 0);
				return;
			}
		}
	}
}


int main(){
	cin >> H >> W >> K;

	for(long long h = 0; h < H; h++){
		string S;
		cin >> S;
		for(long long w = 0; w < W; w++){
			if(S[w] == '.'){
				A[h][w] = OPEN;
			}else if(S[w] == 'S'){
				A[h][w] = 1;
			}else{
				A[h][w] = CLOSED;
			}
		}
	}

	firstmove();

	long long ans = INT_MAX;
	for(long long h = 0; h < H; h++){
		for(long long w = 0; w < W; w++){
			if(A[h][w] != OPEN && A[h][w] != CLOSED){
				long long tmp;
				tmp = h;
				tmp = min(tmp, w);
				tmp = min(tmp, H - h - 1);
				tmp = min(tmp, W - w - 1);
				if(tmp % K == 0){
					ans = min(ans, tmp/K);
				}else{
					ans = min(ans, tmp/K + 1);
				}
			}
		}
	}
	cout << ans + 1 << endl;
}
