#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 10010;
const unsigned long long mod = 1000000000 + 7;
int A[1000][1000];
int D[1000][1000];
bool check[1000][1000];
struct bfs {
  int x, y, dist;
  bfs(int x, int y, int dist) : x(x), y(y), dist(dist) {}
};
int main() {
  int H, W, K, si, sj;
  cin >> H >> W >> K;
  for (int i = 0; i < (int)H; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < (int)W; ++j) {
      check[i][j] = false;
      int tate = min(i, H - i);
      int yoko = min(j, W - j);
      D[i][j] = min(tate, yoko);
      if (s[j] == '#') {
        A[i][j] = 0;
      } else if (s[j] == '.') {
        A[i][j] = 1;
      } else {
        A[i][j] = 2;
        si = i;
        sj = j;
      }
    }
  }
  int res = INT_MAX / 3;
  queue<bfs> que;
  bfs s(si, sj, 0);
  que.push(s);
  while (!que.empty()) {
    bfs b = que.front();
    que.pop();
    check[b.x][b.y] = true;
    res = min(res, D[b.x][b.y]);
    if (b.dist < K) {
      if (check[b.x + 1][b.y] == false && A[b.x + 1][b.y] != 0 && b.x + 1 < W) {
        bfs b1(b.x + 1, b.y, b.dist + 1);
        que.push(b1);
      }
      if (check[b.x - 1][b.y] == false && A[b.x - 1][b.y] != 0 &&
          b.x - 1 > -1) {
        bfs b1(b.x - 1, b.y, b.dist + 1);
        que.push(b1);
      }
      if (check[b.x][b.y + 1] == false && A[b.x][b.y + 1] != 0 && b.y + 1 < H) {
        bfs b1(b.x, b.y + 1, b.dist + 1);
        que.push(b1);
      }
      if (check[b.x][b.y - 1] == false && A[b.x][b.y - 1] != 0 &&
          b.y - 1 > -1) {
        bfs b1(b.x, b.y + 1, b.dist + 1);
        que.push(b1);
      }
    }
  }
  cout << (res == 0 ? 1 : 2 + (res - 1) / K) << endl;
  return 0;
}
