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

#include<string>

#include<cstdio>

#include<cstring>

#include<queue>

using namespace std;

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

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

char ma[888][888];

int n,m,k,dist[888][888],pis[888][888],mn;

queue<pair<int,int> > q;

bool In(int x,int y)

{

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

}

int main()

{

	mn=1e9;

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

	for (int i=0;i<n;i++)

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

	for (int i=0;i<n;i++)

	{

		for (int j=0;j<m;j++)

		{

			pis[i][j]=min(min(i,j),min(n-i-1,m-j-1));

			pis[i][j]=(pis[i][j]+k-1)/k;

		}

	}

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

	for (int i=0;i<n;i++)

	{

		for (int j=0;j<m;j++)

		{

			if (ma[i][j]=='S') 

			{

				q.push(make_pair(i,j));

				dist[i][j]=0;

				mn=min(mn,pis[i][j]+1);

			}

		}

	}

	while(!q.empty())

	{

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

		q.pop();

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

		for (int i=0;i<4;i++)

		{

			int xx=x+dx[i],yy=y+dy[i];

			if (In(xx,yy) && ma[xx][yy]=='.' && dist[xx][yy]==-1)

			{

				mn=min(mn,pis[xx][yy]+1);

				dist[xx][yy]=dist[x][y]+1;

				q.push(make_pair(xx,yy));

			}

		}

	}

	printf("%d\n",mn);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.9741122736733034x):
#include<bits/stdc++.h>



using namespace std;

#define REPP(i,a,b,c) for(int i=a; i<=b; i+=c)

#define REP(i,a,b) REPP(i,a,b,1)

#define REVV(i,a,b,c) for(int i=a; i>=b; i-=c)

#define REV(i,a,b) REVV(i,a,b,1)

#define FOR(i,a) REP(i,0,(int)a-1)

#define FORD(i,a) REV(i,(int)a-1,0)

#define PB push_back

#define POB pop_back

#define FI first

#define SE second

#define MP make_pair

typedef long long ll;

typedef unsigned long long ull;

typedef pair < int , int > pii;

typedef vector < int > vi;

typedef vector < pii > vii;

typedef vector < ll > vl;

typedef pair < ll , ll > pll;



const double PI = acos(-1);

const ll MOD = 1e9 + 7;

const double EPS = 1e-9;

const int INF = 1e9;





ll addMod(ll a, ll b){

	if(a + b >= MOD) return a + b - MOD;

	return a + b;

}



ll mulMod(ll a, ll b){

	return (a*b)%MOD;

}



ll fast(ll a, ll b){

	if(b == 0) return 1;

	ll tmp = fast(a,b/2);

	tmp = mulMod(tmp,tmp);

	if(b & 1) tmp = mulMod(tmp,a);

	return tmp;

}



ll GCD(ll a, ll b){

	if(a > b) return GCD(b,a);

	if(a == 0) return b;

	return GCD(b%a,a);

}



//////////////////////////////////////////////



const int N = 800;



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

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

pii st;

int r,c,k;

string M[N + 5];

bool vis[N + 5][N + 5];



bool inRange(int y, int x){

	return y >= 0 && y < r && x >= 0 && x < c;

}



int solve(){

	queue < pair < pii , int > > q;

	while(!q.empty()) q.pop();

	q.push(MP(st,k));

	int y,x,yy,xx,ret = INF,mn,val,move;

	while(!q.empty()){

		y = q.front().FI.FI;

		x = q.front().FI.SE;

		move = q.front().SE;

		q.pop();

		if(vis[y][x]) continue;

		if(y == 0 || y == r - 1 || x == 0 || x == c-1) return 1;

		vis[y][x] = true;

		mn = min(min(x,c-x-1), min(y, r-y-1));

		val = 0;

		if(mn % k > 0) val = 1;

		val += mn / k;

		ret = min(ret, val+1);

		if(move == 0) continue;

		FOR(i,4){

			yy = y + dy[i];

			xx = x + dx[i];

			if(inRange(yy,xx) && !vis[yy][xx] && M[yy][xx] == '.'){

				q.push(MP(MP(yy,xx),move-1));

			}

		}

	}

	return ret;

}



int main(){

	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> r >> c >> k;

	FOR(i,r){

		cin >> M[i];

		FOR(j,c){

			if(M[i][j] == 'S'){

				st = MP(i,j);

			}

		}

	}

	cout << solve() << endl;

	return 0;

}