// Slower C++ solution:
#include<ctime>

#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstdlib>

#include<cmath> 

#include<cstring> 

#include<cassert>

#include<string>

#include<sstream>

#include<fstream>

#include<deque>

#include<queue>

#include<vector>

#include<map>

#include<list>

#include<stack>

#include<set>

#include<bitset>

#include<iomanip>

#include<utility>

#include<functional>

#include<cctype>

#include<cerrno>

#include<cfloat>

#include<ciso646>

#include<climits>

#include<clocale>

#include<complex>

#include<csetjmp>

#include<csignal>

#include<cstdarg>

#include<cstddef>

#include<cwchar>

#include<cwctype>

#include<exception>

#include<locale>

#include<numeric>

#include<new>

#include<stdexcept>

#include<limits>

using namespace std;



#define ll long long

#define INF 1e9

#define rep(i,n) for(int (i)=0;(i)<n;i++)

#define REP(i,n) for(int (i)=1;(i)<=n;i++)

#define mk(a,b) make_pair(a,b)

#define fi first

#define se second

#define pii pair<int,int>



const int maxn=1005;

int h,w,k,sx,sy,mn=INF;

string s[maxn];

int dist[maxn][maxn];

queue<pii>q;

int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};



int get_dis(int x,int y){

	return min(min(x,h-x-1),min(y,w-y-1));

}



bool onboard(int x,int y){

	return x>=0&&x<h&&y>=0&&y<w;

}



void bfs(){

	q.push(mk(sx,sy));

	rep(i,maxn)rep(j,maxn)dist[i][j]=INF;

	dist[sx][sy]=0;

	mn=INF;

	while(!q.empty()){

		int x=q.front().fi,y=q.front().se;

		q.pop();

		mn=min(mn,get_dis(x,y));

		if(dist[x][y]==k)continue;

		rep(i,4){

			int nx=x+dx[i],ny=y+dy[i];

			if(dist[nx][ny]!=INF||s[nx][ny]=='#'||!onboard(nx,ny))continue;

			dist[nx][ny]=dist[x][y]+1;

			q.push(mk(nx,ny));

		}

	}

}



int main(){

	cin>>h>>w>>k;

	rep(i,h){

		cin>>s[i];

		rep(j,w)if(s[i][j]=='S'){

			sx=i;sy=j;

		}

	}

	bfs();

	cout<<mn/k+(mn%k==0?0:1)+1<<endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.4376838953159554x):
//author: dxm

#include<bits/stdc++.h>

using namespace std;



#define ll long long

#define INF 1e9

#define rep(i,n) for(int (i)=0;(i)<n;i++)

#define REP(i,n) for(int (i)=1;(i)<=n;i++)

#define mp make_pair

#define pb push_back

#define pii pair<int,int>

inline void Fail(){

	printf("0");

	exit(0);

}



const int maxn=805;

int n,m,k,mn=INF,sx,sy;

char grid[maxn][maxn];

int dis[maxn][maxn];

const int dx[]={1,0,-1,0};

const int dy[]={0,1,0,-1};



int Dist(int x,int y){

	return min(x,min(y,min(n-x-1,m-y-1)));

}



bool onboard(int x,int y){

	return x>=0&&x<n&&y>=0&&y<m;

} 



void bfs(){

	memset(dis,-1,sizeof(dis));

	dis[sx][sy]=0;

	queue<pii>q;

	q.push(mp(sx,sy));

	while(!q.empty()){

		int x=q.front().first,y=q.front().second;

		q.pop();

		if(dis[x][y]<=k)mn=min(mn,Dist(x,y));

		rep(i,4){

			int nx=x+dx[i],ny=y+dy[i];

			if(onboard(nx,ny)&&dis[nx][ny]==-1&&grid[nx][ny]=='.'){

				dis[nx][ny]=dis[x][y]+1;

				q.push(mp(nx,ny));

			}

		}

	}

}



int main(){

	scanf("%d%d%d",&n,&m,&k);

	rep(i,n){

		scanf("%s",grid[i]);

		rep(j,m)if(grid[i][j]=='S')sx=i,sy=j;

	}

	bfs();

	printf("%d",1+(mn+k-1)/k);

	return 0;

}



/*

Input:

7 7 1

#######

#######

##...##

#######

##.S.##

###.###

###.###

-----------------

Output:

*/