#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<string>


using namespace std;


int h,w,d[810][810],k,sx,sy,ans=1e9;
queue<pair<int,int> > p;
string s[800];

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


int main()
{
	cin >> h >> w >> k;
	for(int i = 0;i<h;i++)
	{
		cin >> s[i];
		for(int j=0;j<w;j++)
		{
			d[i][j] = -1;
			if(s[i][j] == 'S') sx = i,sy = j;
		}
	}
	p.push(make_pair(sx,sy));
	d[sx][sy] = k;
	while(!p.empty())
	{
		int x =p.front().first,y = p.front().second;
		p.pop();
		ans = min(ans,min(min(x,y),min(h-x-1,w-y-1)));
		if(x==0||y==0||x==h-1||y==w-1)ans=0;
		else for(int r = 0;r<4;r++)
		{
			int tx=x+dx[r],ty=y+dy[r];
			if((s[tx][ty]!='#')&&(d[tx][ty]<d[x][y]-1))
			{
				d[tx][ty]=d[x][y]-1;
				p.push(make_pair(tx,ty));
			}
		}
	}
	cout << (ans + k - 1)/k+1<<endl;
	return 0;
}