// Original C++ solution (slower):
#include <iostream>

#include <fstream>

#include <cstdio>

#include <algorithm>

#include <string>

#include <vector>

#include <queue>

#include <deque>

#include <utility>

#include <map>

#include <set>

#include <cmath>

#include <cstdlib>

#include <ctime>

#include <functional>

#include <sstream>

#include <cstring>

#include <bitset>

#include <stack>

using namespace std;

#define MP make_pair

#define PB push_back

const int INF=1e9;



int n,m,k,sx,sy,ans;

char c[805][805];

int dist[805][805];

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

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



inline void bfs(int X,int Y)

{

	queue <int> q;

	q.push(X);q.push(Y);

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

	dist[X][Y]=0;

	while (!q.empty())

	{

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

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

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

		{

			int tx=x+mx[i];

			int ty=y+my[i];

			if (tx<1 || tx>n || ty<1 || ty>m || dist[tx][ty]!=-1 || c[tx][ty]!='.') continue;

			dist[tx][ty]=dist[x][y]+1;

			q.push(tx);

			q.push(ty);

		}

	} 

}



int main()

{

	cin>>n>>m>>k;

	for (int i=1;i<=n;i++) scanf("%s",c[i]+1);

	for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) if (c[i][j]=='S') {sx=i;sy=j;break;}

	bfs(sx,sy);

	ans=INF;

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

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

			if (dist[i][j]<=k && dist[i][j]!=-1)

			{

		//		cout<<i<<" "<<j<<endl;

				int res=min(i-1,n-i);

				res=min(res,min(j-1,m-j));

				ans=min(ans,res);

			}

	cout<<1+(ans+k-1)/k;

	return 0;

}

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

#include <cstring>

#include <cstdio>

#include <queue>

#include <cmath>

 

#define REDIRECT(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)

#define CLOSEALL() fclose(stdin), fclose(stdout)

 

using namespace std;

 

const int MAXN = 800 + 5;

const int DIRX[] =

{ 1, -1, 0, 0 };

const int DIRY[] =

{ 0, 0, 1, -1 };

 

int H, W, K;

int stX, stY;

char mz[MAXN][MAXN];

bool vist[MAXN][MAXN];

 

int answer = 0x7F7F7F7F;

 

void BFS()

{

	queue<pair<pair<int, int>, int> > Q;

	Q.push(make_pair(make_pair(stX, stY), 0));

 

	while (!Q.empty())

	{

		pair<pair<int, int>, int> cur = Q.front();

		Q.pop();

 

		if(vist[cur.first.first][cur.first.second])

			continue;

		vist[cur.first.first][cur.first.second] = true;

 

		int curAns = min(min(cur.first.first - 1, H - cur.first.first),

				min(cur.first.second - 1, W - cur.first.second));

		curAns = (curAns + K - 1) / K;

		answer = min(answer, curAns + 1);

 

		if (cur.second >= K)

			continue;

 

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

		{

			pair<int, int> newPos = make_pair(cur.first.first + DIRX[i],

					cur.first.second + DIRY[i]);

			if(newPos.first <= 0 || newPos.first > H)

				continue;

			if(newPos.second <= 0 || newPos.second > W)

				continue;

			if(mz[newPos.first][newPos.second] != '#' && !vist[newPos.first][newPos.second])

				Q.push(make_pair(newPos, cur.second + 1));

		}

	}

}

 

int main()

{

 

	//REDIRECT("room");

 

	scanf("%d%d%d", &H, &W, &K);

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

	{

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

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

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

				stX = i, stY = j;

	}

 

	BFS();

 

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

 

	CLOSEALL();

	return 0;

}

// Fastest known solution for this problem: