#include<bits/stdc++.h>

using namespace std;

#define SCD(t) scanf("%d",&t)
#define SCLD(t) scanf("%ld",&t)
#define SCLLD(t) scanf("%lld",&t)
#define SCC(t) scanf("%c",&t)
#define SCS(t) scanf("%s",t)
#define SCF(t) scanf("%f",&t)
#define SCLF(t) scanf("%lf",&t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (long long i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for (long long i=j ; i>=k ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (long long)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readlong long<type>()
const double pi=acos(-1.0);
typedef pair<long long, long long> PII;
typedef vector<long long> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<long long,long long> MPII;
typedef set<long long> SETI;
typedef multiset<long long> MSETI;
typedef long long long long long32;
typedef unsigned long long long ulong long32;
typedef long long long long long long64;
typedef unsigned long long long long  ulong long64;

long long h,w,k,stx,sty,dist[805][805],x[4]={0,0,1,-1},y[4]={1,-1,0,0},mini=1e9;
char g[805][805];

void ff(long long a, long long b) {
	if(a==1 || a==h || b==1 || b==w || dist[a][b]>=k) {
		return;
	}
	for(long long i=0;i<4;i++) {
		if(g[a+x[i]][b+y[i]]=='.' && dist[a+x[i]][b+y[i]]==-1 && dist[a][b]<k) {
			dist[a+x[i]][b+y[i]]=dist[a][b]+1;
			ff(a+x[i],b+y[i]);
		}
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    memset(dist,-1,sizeof(dist));
    cin>>h>>w>>k;
    for(long long i=1;i<=h;i++) {
    	for(long long j=1;j<=w;j++) {
    		cin>>g[i][j];
    		if(g[i][j]=='S') {
    			stx=i;
    			sty=j;
    			if(stx==1 || stx==h || sty==1 || sty==w) {
    				cout<<0<<endl;
    				return 0;
				}
			}
		}
	}
	dist[stx][sty]=0;
	ff(stx,sty);
	for(long long i=1;i<=h;i++) {
    	for(long long j=1;j<=w;j++) {
    		if(dist[i][j]!=-1) {
    			if(i==1 || i==h || j==1 || j==w) {
    				mini=0;
				}
    			mini=min(mini,(max(0,i-1-1))/k+1);
    			mini=min(mini,(max(0,h-i-1))/k+1);
				mini=min(mini,(max(0,j-1-1))/k+1);
				mini=min(mini,(max(0,w-j-1))/k+1);
			}
		}
	}
	cout<<mini+1<<endl;
}