#include <bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define mp make_pair
using namespace std;
int w, h, k;
char ch[810][810];
bool vis[810][810];

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

vector<pair<int, int> > pt;

void dfs(int x, int y, int time)
{
	if(time > k) return;
	pt.push_back(mp(x, y))
	for(int i = 0; i < 4; i ++)
	{
		int u = x + xx[i];
		int v = y + yy[i];
		if(u <= 0) continue;
		if(v <= 0) continue;
		if(u > w) continue;
		if(v > h) continue;
		if(vis[u][v]) continue;
		if(ch[u][v] == '#') continue;
		vis[u][v] = 1;
		dfs(u, v, time + 1);
	}
}

int main()
{
	cin >> h >> w >> k;
	for(int i = 1; i <= w; i ++)
		scanf("%s", ch[i] + 1);
	int stx, sty;
	for(int i = 1; i <= w; i ++)
		for(int j = 1; j <= h; j ++)
			if(ch[i][j] == 'S')
				{stx = i; sty = j; break;}

	if(stx == 1 || sty == 1 || stx == w || sty == h) {cout << 0 << endl; return;}
	//cout << stx << " " << sty << endl;
	vis[stx][sty] = 1;
	dfs(stx, sty, 0);
	int n = pt.size(), dis = 1e7;
	for(int i = 0; i < n; i ++)
	{	
		int x = pt[i].X;
		int y = pt[i].Y;
		dis = min(dis, x - 1);
		dis = min(dis, y - 1);
		dis = min(dis, w - x);
		dis = min(dis, h - y);
	}
	cout << 1 + ceil((double)dis / k);
	return 0;
}
