#include <bits/stdc++.h>
int H, W, K, Si, Sj, ans = 2147483647, a[805][805], f[805][805],
                     fx[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
char s[805];
bool ok(int x, int y) { return (x > 0 && x <= H && y > 0 && y <= W); }
int Abs(int x) { return x < 0 ? -x : x; }
void dfs(int x, int y) {
  f[x][y] = 1;
  int kk = Abs(Si - x) + Abs(Sj - y), kkk;
  for (int F = 0, X, Y, k = 0, k1, k2; F < 4; F++, k = 0)
    if (ok(X = x + fx[F][0], Y = y + fx[F][1]) && !f[X][Y]) {
      if (!a[X][Y]) {
        if (X == 1 || X == H || Y == 1 || Y == W) {
          kkk = kk + 1;
          if (kkk % K == 0)
            kkk /= K;
          else
            kkk = kkk / K + 1;
          if (ans > kkk) ans = kkk;
        } else
          dfs(X, Y);
      } else {
        if (F == 0) k = W - Y + 1;
        if (F == 1) k = H - X + 1;
        if (F == 2) k = Y;
        if (F == 3) k = X;
        if (kk % K == 0)
          kk /= K;
        else
          kk = kk / K, k += kk % K;
        if (k % K == 0)
          k /= K;
        else
          k = k / K + 1;
        k += kk;
        if (ans > k + 1) ans = k + 1;
      }
    }
}
int main() {
  scanf("%d%d%d", &H, &W, &K);
  for (int i = 1; i <= H; i++) {
    scanf("%s", s);
    for (int j = 1; j <= W; j++)
      a[i][j] =
          ((s[j - 1] == '#') ? 1 : (s[j - 1] == '.' ? 0 : (Si = i, Sj = j)));
  }
  if (Si == 0 || Sj == 0 || Si == H || Sj == W) {
    puts("0");
    return 0;
  }
  dfs(Si, Sj);
  printf("%d", ans);
}
