#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;
}