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

typedef int ll;

#define int long long
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define all(x) x.begin(),x.end()

typedef pair<int,int>pii;
typedef pair<int,pair<int,int> > piii;

const int mod=1e9+7;

int POWER[65];
int power(int a, int b) {int ret=1;while(b) {if(b&1) ret*=a;a*=a;if(ret>=mod) ret%=mod;if(a>=mod) a%=mod;b>>=1;}return ret;}
int inv(int x) {return power(x,mod-2);}

void precompute() {
	POWER[0]=1;
	for(int i=1;i<63;i++) POWER[i]=POWER[i-1]<<1LL;
}

const int maxn=855;
int sr=-1,sc=-1;
bool vis[maxn][maxn];
int h,w,k,dis=100000000;
vector<string>v;

queue<int>xq,yq,le;
bool chk(int x , int y , int z) {
	if(x>=0 && x<h && y>=0 && y<w) {
		if(vis[x][y]==0 && v[x][y]!='#') {
			vis[x][y]=1;
			int d=min(min(x,h-x-1),min(y,w-y-1));
			dis=min(dis,d);
			if(z<=k) {
				xq.push(x);
				yq.push(y);
				le.push(z);
			}
			return 1;
		}
	}
	return 0;
}
void bfs(int i , int j) {
	chk(i,j,0);
	while (!xq.empty()) {
		int a=xq.front(),b=yq.front(),c=le.front();
		chk(a-1,b,c+1);
		chk(a+1,b,c+1);
		chk(a,b-1,c+1);
		chk(a,b+1,c+1);
		xq.pop(),yq.pop(),le.pop();
	}
}

ll main() {
	// freopen("Task.in","r",stdin);freopen("Task.out","w",stdout);

	std:ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>h>>w>>k;

	for (int i=0 ; i<h ; i++) {
		string s;
		cin>>s;

		v.pb(s);
	}

	for (int i=0 ; i<v.size() ; i++) {
		for (int j=0 ; j<v[i].size() ; j++) {
			if(v[i][j]=='S') {
				sr=i,sc=j;
			}
		}
	}

	bfs(sr,sc);
	int ans=dis/k+1;
	int d=min(min(sr,h-sr-1),min(sc,w-sc-1));
	int ans2=d/k;
	if(d%k!=0)
		ans2++;
	if(dis%k!=0)
		ans++;
	ans=min(ans,ans2)
	cout<<ans<<endl;

}