#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 move[5][3];
bool visited[848][848];
int r,c,k;
int sx,sy;
int a[848][848];
string s;

void dfs(int x,int y,int step)
{
	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+move[i][1];yy=y+move[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;
	move[1][1]=1;move[1][2]=0;
	move[2][1]=0;move[2][2]=1;
	move[3][1]=-1;move[3][2]=0;
	move[4][1]=0;move[4][2]=-1;
	cin>>r>>c>>k;
	for (i=1;i<=r;i++)
	{
		cin>>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;
			}
		}
	}
	dfs(sx,sy,0);
	int fans=1+(ans+k-1)/k;
	cout<<fans<<endl;
	return 0;
}

/*
3 3 3
#.#
#S.
###

3 3 3
###
#S#
###

7 7 2
#######
#######
##...##
###S###
##.#.##
###.###
#######
*/