#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <tuple>
#include <array>
#include <iomanip>

using namespace std;

pair<int, int> getStart(const vector<string> &table) {
	for(int i = 0; i < table.size(); i++){
		for(int j = 0; j < table[i].length(); j++) {
			if(table[i][j] == 'S') {
				return make_pair(i, j);
			}
		}
	}
	return make_pair(-1, -1);
}

int dist(int sy, int sx, int H, int W) {
	int ans = 10000;
	ans = min(ans, sx);
	ans = min(ans, W - 1 - sx);
	ans = min(ans, sy);
	ans = min(ans, H - 1 - sy);
	return ans;
}

array<array<bool, 801>, 801> visited;

tuple<int, int, int> dfs(const vector<string> &table, const int sy, const int sx, const int K) {
	const int W = table[0].length();
	const int H = table.size();
	int bestY = sy;
	int bestX = sx;
	int bestDist = dist(sy, sx, H, W);
	
	vector<int> dy = {0, 1, 0, -1};
	vector<int> dx = {1, 0, -1, 0};

    vector<tuple<int, int, int>> vs;
	vs.push_back(make_tuple(0, sy, sx));
	
	while(!vs.empty()) {
		int y, x, turn;
		tie(turn, y, x) = vs.back();
		vs.pop_back();
		visited[y][x] = true;
		int d = dist(y, x, H, W);
		if(d < bestDist) {
			bestDist = d;
			bestY = y;
			bestX = x;
		}

		for(int i = 0; i < 4; i++) {
			int ny = y + dy[i];
			int nx = x + dx[i];
			if(turn < K &&
			   0 <= ny && ny < H &&
			   0 <= nx && nx < W &&
			   table[ny][nx] != '#' &&
			   !visited[ny][nx]) {
			   	
			    vs.push_back(make_tuple(turn + 1, ny, nx));
			}
		}
	}
	return make_tuple(bestDist, bestY, bestX);
}

int main() {
	
	cin.tie(0);
    ios::sync_with_stdio(false);
	
	for(int i = 0; i <= 800; i++) {
		for(int j = 0; j <= 800; j++) {
			visited[i][j] = false;
		}
	}
	
	int h, w, k;
	cin >> h >> w >> k;
	vector<string> table(h);
	getline(cin, table[0]);
	for(int i = 0; i < h; i++) {
		getline(cin, table[i]);
	}
	int sy, sx;
	tie(sy, sx) = getStart(table);
	
	int turn, bestY, bestX;
	tie(turn, bestY, bestX) = dfs(table, sy, sx, k);
	cout << ((turn + k - 1) / k + 1) << endl;
	
	return 0;
}