#include <bits/stdc++.h>
using namespace std;
struct punct {
  int i, j;
} q[800 * 800 + 5];
int n, i, j, v[800 + 5][800 + 5], m, ok, filled[800 + 5][800 + 5], r,
    di[] = {0, -1, 0, 1}, dj[] = {1, 0, -1, 0}, nrq;
char c;
void myfill() {
  int i1, j1, i2, j2, dif;
  for (r = 1; r <= nrq; r++) {
    i1 = q[r].i;
    j1 = q[r].j;
    for (dif = 0; dif <= 3; dif++) {
      i2 = q[r].i + di[dif];
      j2 = q[r].j + dj[dif];
      if (i2 >= 1 && i2 <= n && j2 >= 1 && j2 <= m && v[i2][j2] == 0 &&
          filled[i2][j2] == 0) {
        q[++nrq] = {i2, j2};
        filled[i2][j2] = filled[i1][j1] + 1;
      }
    }
  }
}
inline bool vecin0(int i, int j) {
  int i2, j2;
  for (int dif = 0; dif <= 3; dif++) {
    i2 = i + di[dif];
    j2 = j + dj[dif];
    if (i2 >= 1 && i2 <= n && j2 >= 1 && j2 <= m && v[i2][j2] == -1) return 1;
    if (i2 == 0 || i2 == n + 1 || j2 == 0 || j2 == m + 1) return 1;
  }
  return 0;
}
int closest(int i, int j) { return min(i - 1, min(n - i, min(j - 1, m - j))); }
int main() {
  int k;
  cin >> n >> m >> k;
  int i1, j1;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      cin >> c;
      if (c == '.')
        v[i][j] = 0;
      else if (c == '#')
        v[i][j] = -1;
      else
        v[i][j] = 0, i1 = i, j1 = j;
    }
  filled[i1][j1] = 1;
  q[1] = {i1, j1};
  nrq = 1;
  myfill();
  int mini = n * m + 1, nrpasi;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      if (filled[i][j] && vecin0(i, j)) {
        nrpasi = (filled[i][j] + k - 2) / k;
        if (nrpasi == 0) nrpasi = 1;
        nrpasi += (closest(i, j) + k - 1) / k;
        if (nrpasi < mini) mini = nrpasi;
      }
    }
  cout << mini << '\n';
  return 0;
}
