#include <bits/stdc++.h>
using namespace std;
struct Point {
  int x, y;
  Point(int _x, int _y) : x(_x), y(_y) {}
  bool operator<(const Point &a) const { return x == a.x ? y < a.y : x < a.x; }
};
int H, W, K;
char A[1000][1000];
int sx, sy;
bool used[1000][1000];
int cnt[1000][1000];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
void bfs() {
  queue<pair<Point, int> > que;
  que.push(make_pair(Point(sx, sy), K));
  while (que.size() > 0) {
    Point p = que.front().first;
    int dep = que.front().second - 1;
    que.pop();
    if (p.x < 0 || p.y < 0 || p.x >= W || p.y >= H) continue;
    if (used[p.y][p.x]) continue;
    if (cnt[p.y][p.x] < 0) continue;
    if (A[p.y][p.x] == '#') continue;
    used[p.y][p.x] = true;
    cnt[p.y][p.x] = dep;
    for (int(i) = 0; (i) < (int)(4); (i)++) {
      que.push(make_pair(Point(dx[i] + p.x, dy[i] + p.y), dep));
    }
  }
}
int main() {
  cin >> H >> W >> K;
  for (int(i) = 0; (i) < (int)(H); (i)++) scanf("%s", A[i]);
  sx = sy = -1;
  for (int(i) = 0; (i) < (int)(H); (i)++)
    for (int(j) = 0; (j) < (int)(W); (j)++) {
      if (A[i][j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  for (int(i) = 0; (i) < (int)(1000); (i)++)
    for (int(j) = 0; (j) < (int)(1000); (j)++) used[i][j] = false;
  for (int(i) = 0; (i) < (int)(1000); (i)++)
    for (int(j) = 0; (j) < (int)(1000); (j)++) cnt[i][j] = 1e9;
  bfs();
  for (int(i) = 0; (i) < (int)(H); (i)++) {
    if (cnt[i][W - 1] < K || cnt[i][0] < K) {
      cout << 1 << endl;
      return 0;
    }
  }
  for (int(i) = 0; (i) < (int)(W); (i)++) {
    if (cnt[H - 1][i] < K || cnt[0][i] < K) {
      cout << 1 << endl;
      return 0;
    }
  }
  int ans = 1e9;
  for (int(i) = 0; (i) < (int)(H); (i)++)
    for (int(j) = 0; (j) < (int)(W); (j)++) {
      if (cnt[i][j] >= 0) {
        ans = min(ans, (i + 1) / K + 2);
        ans = min(ans, (j + 1) / K + 2);
        ans = min(ans, (H - i) / K + 2);
        ans = min(ans, (W - j) / K + 2);
      }
    }
  cout << ans << endl;
  return 0;
}
