#include <bits/stdc++.h>
using namespace std;
struct pos {
  int x;
  int y;
  int d;
};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<bool>> a(h, vector<bool>(w));
  char c;
  int sx, sy;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c;
      if (c == '#') {
        a[i][j] = 1;
      } else if (c == '.') {
        a[i][j] = 0;
      } else {
        a[i][j] = 0;
        sx = j, sy = i;
      }
    }
  }
  vector<int> houkou = {0, 1, 0, -1, 0};
  vector<vector<bool>> arrived(h, vector<bool>(w, 0));
  arrived[sy][sx] = 0;
  queue<struct pos> bfs, bfs2;
  bfs.push({sx, sy, 0});
  int nx, ny;
  struct pos now;
  while (!bfs.empty()) {
    now = bfs.front();
    bfs.pop();
    for (int i = 0; i < 4; i++) {
      nx = now.x + houkou[i];
      ny = now.y + houkou[i + 1];
      if (now.d == k) {
        if (0 <= nx && nx < w && 0 <= ny && ny < h) {
          if (nx == 0 || nx == w - 1 || ny == 0 || ny == h - 1) {
            cout << 2 << endl;
            return 0;
          }
          if (!arrived[ny][nx]) {
            arrived[ny][nx] = 1;
            bfs2.push({nx, ny, k + 1});
          }
        }
      } else {
        if (0 <= nx && nx < w && 0 <= ny && ny < h) {
          if (nx == 0 || nx == w - 1 || ny == 0 || ny == h - 1) {
            cout << (a[ny][nx] ? 2 : 1) << endl;
            return 0;
          }
          if (!arrived[ny][nx]) {
            arrived[ny][nx] = 1;
            if (a[ny][nx]) {
              bfs2.push({nx, ny, k + 1});
            } else {
              bfs.push({nx, ny, now.d + 1});
            }
          }
        }
      }
    }
  }
  while (!bfs2.empty()) {
    now = bfs2.front();
    bfs2.pop();
    for (int i = 0; i < 4; i++) {
      nx = now.x + houkou[i];
      ny = now.y + houkou[i + 1];
      if (0 <= nx && nx < w && 0 <= ny && ny < h) {
        if (nx == 0 || nx == w - 1 || ny == 0 || ny == h - 1) {
          cout << (now.d + k - 1) / k << endl;
          return 0;
        }
        if (!arrived[ny][nx]) {
          arrived[ny][nx] = 1;
          bfs2.push({nx, ny, now.d + 1});
        }
      }
    }
  }
  return 0;
}
