#include <bits/stdc++.h>
using namespace std;
const int vx[4] = {0, 1, 0, -1}, vy[4] = {1, 0, -1, 0};
int H, W, K;
int field[800 + 10][800 + 10];
bool visited[800 + 10][800 + 10];
int nearest(int x, int y) {
  int ret = H * W;
  ret = min(ret, (int)ceil((double)x / K));
  ret = min(ret, (int)ceil((double)y / K));
  ret = min(ret, (int)ceil((double)(H - 1 - x) / K));
  ret = min(ret, (int)ceil((double)(W - 1 - y) / K));
  return ret;
}
int main() {
  cin >> H >> W >> K;
  int sx = 0, sy = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == '.')
        field[i][j] = 0;
      else if (c == '#')
        field[i][j] = 1;
      else
        field[i][j] = 2, sx = i, sy = i;
    }
  }
  int ans = H * W;
  queue<tuple<int, int, int> > q;
  q.push(make_tuple(sx, sy, 0));
  visited[sx][sy] = true;
  while (q.size()) {
    int x = get<0>(q.front()), y = get<1>(q.front()),
        length = get<2>(q.front());
    q.pop();
    if (length == K) continue;
    if (x == 0 || y == 0 || x == H - 1 || y == W - 1)
      ans = min(ans, 1);
    else
      ans = min(ans, 1 + nearest(x, y));
    for (int i = 0; i < 4; i++) {
      int nx = x + vx[i], ny = y + vy[i];
      if (nx < 0 || ny < 0 || nx >= H || ny >= W) continue;
      if (field[nx][ny] == 1 || visited[nx][ny]) continue;
      visited[nx][ny] = true;
      q.push(make_tuple(nx, ny, length + 1));
    }
  }
  cout << ans << endl;
  return 0;
}
