#include <bits/stdc++.h>
using namespace std;
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};
pair<int, int> st;
queue<pair<int, int> > Q;
void dfs(pair<int, int> 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++) {
    pair<int, int> 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()) {
    pair<int, int> 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;
}
