#include <bits/stdc++.h>
using namespace std;
int const H = 841;
int const W = 841;
int const INF = 1e9 + 41;
int h, w, k;
int ma[H][W], d[H][W], was[H][W], f[H][W];
int sx, sy;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
deque<pair<int, int> > q;
bool canMove(int ny, int nx) {
  if (ny >= h || ny < 0) return false;
  if (nx >= w || nx < 0) return false;
  if (ma[ny][nx] == 1) return false;
  return true;
}
int calc(int y, int x) {
  int ret = min(x, y) / k + (min(x, y) % k != 0 ? 1 : 0);
  return ret;
}
void solve() {
  cin >> h >> w >> k;
  k = min(k, max(h, w));
  for (int i = 0; i < h; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < w; ++j) {
      if (s[j] == '.') {
        ma[i][j] = 0;
      } else if (s[j] == '#') {
        ma[i][j] = 1;
      } else {
        sy = i;
        sx = j;
      }
    }
  }
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) d[i][j] = INF;
  d[sy][sx] = 0;
  q.push_back(make_pair(sy, sx));
  was[sy][sx] = 1;
  while ((int)(q.size())) {
    pair<int, int> cur = q.front();
    q.pop_front();
    int x = cur.second;
    int y = cur.first;
    if (d[y][x] == k) continue;
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (canMove(ny, nx) && !was[ny][nx]) {
        d[ny][nx] = 1 + d[y][x];
        was[ny][nx] = 1;
        q.push_back(make_pair(ny, nx));
      }
    }
  }
  int ans = INF;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (was[i][j]) {
        ans = min(ans, 1 + calc(i, j));
      }
  cout << ans << endl;
}
int main() {
  solve();
  return 0;
}
