// 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: 4.172557341766582x):
#include <cstdio>

#include <iostream>

#include <cstring>

#include <string>

#include <cmath>

#include <algorithm>

#include <cstdlib>

#include <utility>

#include <map>

#include <stack>

#include <set>

#include <vector>

#include <queue>

#include <deque>

#include <sstream>

#include <bitset> 

#define x first

#define y second

#define mp make_pair

#define pb push_back

#define LL long long

#define ld long double

#define Pair pair<int,int>

#define LOWBIT(x) x & (-x)

using namespace std;



const int INF=0x7ffffff;



int ans=INF;

int movee[5][3];

bool visited[848][848];

int r,c,k;

int sx,sy;

int a[848][848];

char s[1000];

queue<int> q;



void bfs(int x,int y,int step)

{

	q.push(x);q.push(y);q.push(0);

	int cur_ans,cx,cy,cd,xx,yy,dd;

	visited[x][y]=true;

	while (!q.empty())

	{

		cx=q.front();q.pop();

		cy=q.front();q.pop();

		cd=q.front();q.pop();

		//visited[cx][cy]=true;

		cur_ans=cx-1;

		cur_ans=min(cur_ans,cy-1);

		cur_ans=min(cur_ans,r-cx);

		cur_ans=min(cur_ans,c-cy);

		ans=min(ans,cur_ans);

		if (cd==k) continue;

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

		{

			xx=cx+movee[i][1];yy=cy+movee[i][2];

			if (xx<1 || xx>r || yy<1 || yy>c) continue;

			if (a[xx][yy]==1 || visited[xx][yy]) continue;

			visited[xx][yy]=true;q.push(xx);q.push(yy);q.push(cd+1);

		}

	}

	/*int cur_ans=x-1;

	cur_ans=min(cur_ans,y-1);

	cur_ans=min(cur_ans,r-x);

	cur_ans=min(cur_ans,c-y);

	ans=min(ans,cur_ans);

	visited[x][y]=true;

	if (step==k) return;

	int xx,yy,i;

	for (i=1;i<=4;i++)

	{

		xx=x+movee[i][1];yy=y+movee[i][2];

		if (xx<1 || xx>r || yy<1 || yy>c) continue;

		if (a[xx][yy]==1 || visited[xx][yy]) continue;

		dfs(xx,yy,step+1);

	}*/

}



int main ()

{

	//ios::sync_with_stdio(false);

	int i,j;

	movee[1][1]=1;movee[1][2]=0;

	movee[2][1]=0;movee[2][2]=1;

	movee[3][1]=-1;movee[3][2]=0;

	movee[4][1]=0;movee[4][2]=-1;

	scanf("%d%d%d",&r,&c,&k);

	for (i=1;i<=r;i++)

	{

		scanf("%s",s);

		for (j=1;j<=c;j++)

		{

			if (s[j-1]=='#') a[i][j]=1;

			if (s[j-1]=='.') a[i][j]=2;

			if (s[j-1]=='S')

			{

				sx=i;sy=j;

				a[i][j]=2;

			}

		}

	}

	bfs(sx,sy,0);

	int fans=1+(ans+k-1)/k;

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

	return 0;

}



/*

3 3 3

#.#

#S.

###



3 3 3

###

#S#

###



7 7 2

#######

#######

##...##

###S###

##.#.##

###.###

#######

*/