#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void amin(T &x, U y) {
  if (y < x) x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y) {
  if (x < y) x = y;
}
long long int MOD = 1000000007;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int H, W, K;
int grid[900][900];
int used[900][900];
int ans;
int sx, sy;
void dfs(int x, int y, int cnt) {
  used[x][y] = min(used[x][y], cnt);
  int hoge = min(min(H - 1 - x, x), min(W - 1 - y, y));
  ans = min(ans, (hoge + K - 1) / K + 1);
  cnt++;
  if (cnt <= K) {
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W) {
        if (grid[nx][ny] == 0 && used[nx][ny] > cnt) {
          dfs(nx, ny, cnt);
        }
      }
    }
  }
}
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < W; j++) {
      used[i][j] = H * W + 1;
      if (str[j] == '#') {
        grid[i][j] = 1;
      } else if (str[j] == '.') {
        grid[i][j] = 0;
      } else {
        sx = i;
        sy = j;
        grid[i][j] = 0;
      }
    }
  }
  ans = H * W + 1;
  dfs(sx, sy, 0);
  cout << ans << endl;
}
