#include <iostream>
#include <queue>
#include <algorithm> 

using namespace std;

typedef pair<int, int> pii;

const int INF = 1e8 + 8;
const int MAXN = 805;
bool vis[MAXN][MAXN];
bool brd[MAXN][MAXN];
int H, W, K, ans;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
pii st;
queue<pii> Q;

void dfs(pii croom, int lefts) {
	if (lefts == 0) return;
	if (vis[croom.first][croom.second]) {
		return;
	} else {
		vis[croom.first][croom.second] = true;
		Q.push(croom);
	}
	for (int i = 0; i < 4; i++) {
		pii nroom = croom;
		nroom.first += dx[i];
		nroom.second += dy[i];
		if (nroom.first < 0 || nroom.first >= H || nroom.second < 0 || nroom.second >= W) continue;
		if (brd[nroom.first][nroom.second]) {
			dfs(nroom, lefts - 1);
		}
	}
}

int main() {
	cin >> H >> W >> K;
	char c;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> c;
			if (c == '.') {
				brd[i][j] = true;
			} else if (c == '#') {
				brd[i][j] = false;
			} else {
				brd[i][j] = true;
				st.first = i;
				st.second = j;
			}
		}
	}
	dfs(st, K + 1);
	ans = INF;
	while (!Q.empty()) {
		pii croom = Q.front();
		Q.pop();
		ans = min(ans, (min(min(croom.first, H - 1 - croom.first), min(croom.second, W - 1 - croom.second)) + K - 1) / K + 1);
	}
	cout << ans << '\n';
	return 0;
}