#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <functional>
using namespace std;

#define INF (int)1e7
int N, M, K;
vector<vector<int>>Room;
int status[800][800];
int cost[800][800];
int v[] = { -1, 0, 1, 0 };
int h[] = { 0, 1, 0, -1 };

vector<pair<int,int>> dijkstra(pair<int,int>s) {
	vector<pair<int, int>>pos;

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			status[i][j] = 0;
			cost[i][j] = INF;
		}
	}
	cost[s.first][s.second] = 0;
	status[s.first][s.second] = 1;

	priority_queue<pair<int, pair<int,int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>>U;
	U.push(make_pair(cost[s.first][s.second], s));
	while (!U.empty()) {
		//探索が終わってない中でコスト最小の点
		pair<int,int>u = U.top().second; U.pop();

		if (status[u.first][u.second] == -1) continue;
		pos.push_back(u);
		status[u.first][u.second] = -1;

		//コスト最少の点を探る
		for (int i = 0; i < 4; i++) {
			if (status[u.first + v[i]][u.second + h[i]] != -1 && u.first + v[i]>= 0 && u.second + h[i]>= 0 && u.first + v[i] < N && u.second + h[i] < M) {
				if (Room[u.first + v[i]][u.second + h[i]])continue;
				if (cost[u.first + v[i]][u.second + h[i]] == K)continue;
				if (cost[u.first + v[i]][u.second + h[i]] > cost[u.first][u.second] + 1) {
					cost[u.first + v[i]][u.second + h[i]] = cost[u.first][u.second] + 1;
					status[u.first + v[i]][u.second + h[i]] = 1;
					U.push(make_pair(cost[u.first + v[i]][u.second + h[i]], make_pair(u.first + v[i], u.second + h[i])));
				}
			}
		}
	}

	return pos;
}

int main()
{
	cin >> N >> M >> K;
	Room.resize(N);
	pair<int, int>pos;
	for (int i = 0; i < N; i++) {
		Room[i].resize(M);
		for (int j = 0; j < M; j++) {
			char c; cin >> c;
			if (c == '#')Room[i][j] = 1;
			else if (c == 'S')pos = make_pair(i, j);
		}
	}

	vector<pair<int,int>>nextpos = dijkstra(pos);
	vector<int>ans;
	for (int k = 0; k < nextpos.size(); k++) {
		int dist = min(min(nextpos[k].first,N - 1 - nextpos[k].first), min(nextpos[k].second, M - 1 - nextpos[k].second));
		ans.push_back(dist);
	}
	sort(ans.begin(), ans.end());

	cout << int(ceil((double)ans[0] / K)) + 1 << endl;

    return 0;
}

