#include <bits/stdc++.h>
using namespace std;
long long h, w, k;
long long const MAX = 808;
long long grid[MAX][MAX];
long long dist[MAX][MAX];
struct point {
  long long x;
  long long y;
};
long long f(point p) {
  long long x = p.x, y = p.y;
  long long x1, x2, y1, y2;
  x1 = (x - 1) / k + (0 < (x - 1) % k);
  x2 = (h - x) / k + (0 < (h - x) % k);
  y1 = (y - 1) / k + (0 < (y - 1) % k);
  y2 = (w - y) / k + (0 < (w - y) % k);
  long long ans = min({x1, x2, y1, y2});
  return ans;
}
int main() {
  cin >> h >> w >> k;
  for (long long i = 0; i <= MAX - 1; i++) {
    for (long long j = 0; j <= MAX - 1; j++) {
      dist[i][j] = -1;
    }
  }
  point start;
  for (long long i = 0; i <= h + 1; i++) {
    for (long long j = 0; j <= w + 1; j++) {
      if (i == 0 || i == h + 1 || j == 0 || j == w + 1) {
        grid[i][j] = 1;
      } else {
        char c;
        cin >> c;
        if (c == '.') {
          grid[i][j] = 0;
        } else if (c == '#') {
          grid[i][j] = 1;
        } else {
          grid[i][j] = 0;
          start = {i, j};
          dist[i][j] = 0;
        }
      }
    }
  }
  long long dx[4] = {1, 0, -1, 0};
  long long dy[4] = {0, 1, 0, -1};
  queue<point> qu;
  qu.push(start);
  long long ans = f(start);
  while (!qu.empty()) {
    point cur = qu.front();
    long long cur_dist = dist[cur.x][cur.y];
    qu.pop();
    for (long long dir = 0; dir <= 3; dir++) {
      point next = {cur.x + dx[dir], cur.y + dy[dir]};
      if (grid[next.x][next.y] == 0 && dist[next.x][next.y] == -1 &&
          cur_dist <= k) {
        dist[next.x][next.y] = cur_dist + 1;
        qu.push(next);
        ans = min(ans, f(next));
      }
    }
  }
  cout << ans + 1 << endl;
}
