#include <iostream>
#include <queue>

using namespace std;
typedef pair<int,int> pii;


int H,W,K;
char grid[805][805];
int dist[805][805];
int x0,y0;

int main() {
	cin >> H >> W >> K;
	for (int i=0; i<H; i++) {
		for (int j=0; j<W; j++) {
			cin >> grid[i][j];
			if (grid[i][j] == 'S') {
				x0 = i;
				y0 = j;
			}
			dist[i][j] = -1;
		}
	}

	int mind = H*W;
	queue<pii> q;
	q.push(pii(x0,y0));
	while (q.size() > 0) {
		pii cur = q.front();
		q.pop();
		int x=cur.first, y=cur.second;
		if (x < 0 || x >= H || y < 0 || y >= W) continue;
		if (dist[x][y] >= 0 || grid[x][y] == '#') continue;
		dist[x][y] = min(min(x,y),min(H-x-1,W-y-1));
		mind = min(mind,dist[x][y]);
		q.push(pii(x-1,y));
		q.push(pii(x+1,y));
		q.push(pii(x,y-1));
		q.push(pii(x,y+1));
	}

	//cout << mind << endl;
	
	if ((mind+K-1)/K < (dist[x0][y0]+K-1)/K)
		cout << (dist[x0][y0]+K-1)/K << "\n";
	else
		cout << (dist[x0][y0]+K-1)/K + 1 << "\n";
}