// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define pb push_back

#define pf push_front

#define eb emplace_back

#define PF pop_front

#define PB pop_back

#define mp make_pair

#define s second

#define f first

#define mt make_tuple

#define foreach(it, a) for(__typeof(a.begin()) it=a.begin(); it!=a.end(); it++)

#define forn(i, a) for(int i=0; i<a; i++)

#define INF 1e9

#define all(x) x.begin(), x.end()

#define MOD 1e9+7 

 

typedef long long ll;

typedef pair<int,int> pi;

typedef pair<pi,int> pii;

typedef unsigned long long ull;

typedef vector<int> vi;

typedef unsigned int uint;

 

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

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

 

int n, m, stx, sty, step[805][805], mx, res=2e9;

char grid[805][805];

queue<int> q;

 

inline bool out(int x, int y)

{

	return x<0||x==n||y<0||y==m;

}

 

void Init()

{

	cin>>n>>m>>mx;

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

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

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

		{

			cin>>grid[i][j];

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

				stx=i, sty=j;

		}

		

	q.push(stx), q.push(sty);

	

	step[stx][sty]=0;

	while(!q.empty())

	{

		int x=q.front();	q.pop();

		int y=q.front();	q.pop();

		if(step[x][y]==mx+1)

			break;

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

		{

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

			if(!out(xx, yy)&&step[xx][yy]==-1&&grid[xx][yy]!='#')

			{

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

				q.push(xx);

				q.push(yy);

			}

		}

	}

	while(!q.empty())

		q.pop();

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

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

			if(step[i][j]<=mx&&step[i][j]!=-1)

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

}

 

int main() 

{

	Init();

	while(!q.empty())

	{

		int x=q.front();	q.pop();

		int y=q.front();	q.pop();

		res=min(res, (min(min(n-x-1, x), min(y, m-y-1))+mx-1)/mx);

	}

	printf("%d", res+1);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<vector>

#include<queue>

#define SF scanf

#define PF printf

#define Mkp make_pair

#define MAXN 810

using namespace std;

typedef pair<int,int> pii;

char s[MAXN][MAXN];

int h,w,k;

int vis[MAXN][MAXN],dist[MAXN][MAXN];

queue<pair<int,int> > q;

bool allow(int x,int y){

	if(x<=0||y<=0||x>h||y>w||s[x][y]=='#'||vis[x][y]==1)

		return 0;

	return 1;	

}

int t[6][4]={{0,1},{0,-1},{1,0},{-1,0}};

void bfs(int x,int y){

	vis[x][y]=1;

	q.push(Mkp(x,y));

	while(!q.empty()){

		pii newq=q.front();

		q.pop();

		if(dist[newq.first][newq.second]==k)

			break;

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

			int xx=newq.first+t[i][0];

			int yy=newq.second+t[i][1];

			if(allow(xx,yy)==0)

				continue;

			vis[xx][yy]=1;

			dist[xx][yy]=dist[newq.first][newq.second]+1;

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

		}

	}

}

int mindist(int x,int y){

	return min(min((x+k-2)/k,(y+k-2)/k),min((h-x+k-1)/k,(w-y+k-1)/k));	

}

int main(){

	int stx,sty;

	SF("%d%d%d",&h,&w,&k);

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

		SF("%s",s[i]+1);

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

		for(int j=1;j<=w;j++)

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

				stx=i;

				sty=j;

			}

	bfs(stx,sty);

	int ans=1+mindist(stx,sty);

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

		for(int j=1;j<=w;j++)

			if(vis[i][j]==1){

				ans=min(ans,1+mindist(i,j));

			}

	PF("%d",ans);

}

// Fastest known solution for this problem: