#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
int dist[810][810] = {0};
int calc_dist(int i, int j, int H, int W) {
  return min(min(i, H - 1 - i), min(j, W - 1 - j));
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<bool>> is_ok(H);
  string s;
  int si, sj;
  for (int i = 0; i < H; i++) {
    cin >> s;
    for (int j = 0; j < W; j++) {
      if (s[j] == '#') {
        is_ok[i].push_back(false);
      } else if (s[j] == '.') {
        is_ok[i].push_back(true);
      } else {
        is_ok[i].push_back(true);
        si = i;
        sj = j;
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      dist[i][j] = -1;
    }
  }
  dist[si][sj] = 0;
  int min_dist = calc_dist(si, sj, H, W);
  queue<pair<int, int>> q;
  q.push(make_pair(si, sj));
  while (!q.empty()) {
    pair<int, int> next = q.front();
    int ci = next.first;
    int cj = next.second;
    q.pop();
    if (ci - 1 >= 0 && is_ok[ci - 1][cj]) {
      if (dist[ci - 1][cj] >= 0) {
        dist[ci - 1][cj] = dist[ci][cj] + 1;
        if (dist[ci - 1][cj] <= K) {
          chmin(min_dist, calc_dist(ci - 1, cj, H, W));
        }
        q.push(make_pair(ci - 1, cj));
      }
    }
    if (ci + 1 < H && is_ok[ci + 1][cj]) {
      if (dist[ci + 1][cj] < 0) {
        dist[ci + 1][cj] = dist[ci][cj] + 1;
        if (dist[ci + 1][cj] <= K) {
          chmin(min_dist, calc_dist(ci + 1, cj, H, W));
        }
        q.push(make_pair(ci + 1, cj));
      }
    }
    if (cj - 1 >= 0 && is_ok[ci][cj - 1]) {
      if (dist[ci][cj - 1] < 0) {
        dist[ci][cj - 1] = dist[ci][cj] + 1;
        if (dist[ci][cj - 1] <= K) {
          chmin(min_dist, calc_dist(ci, cj - 1, H, W));
        }
        q.push(make_pair(ci, cj - 1));
      }
    }
    if (cj + 1 < W && is_ok[ci][cj + 1]) {
      if (dist[ci][cj + 1] < 0) {
        dist[ci][cj + 1] = dist[ci][cj] + 1;
        if (dist[ci][cj + 1] <= K) {
          chmin(min_dist, calc_dist(ci, cj + 1, H, W));
        };
        q.push(make_pair(ci, cj + 1));
      }
    }
  }
  cout << 1 + (min_dist + K - 1) / K << endl;
  return 0;
}
