#include <bits/stdc++.h>
using namespace std;
int dp[808][808];
struct st {
  int y, x;
  st() {}
  st(int y, int x) : y(y), x(x) {}
};
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  string a[h];
  for (int i = 0; i < h; i++) cin >> a[i];
  int sy, sx;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') sy = i, sx = j;
    }
  }
  int ans = 8000;
  int ay[] = {0, 0, 1, -1};
  int ax[] = {1, -1, 0, 0};
  memset(dp, -1, sizeof(dp));
  queue<st> q;
  q.emplace(sy, sx);
  dp[sy][sx] = 0;
  while (!q.empty()) {
    st p = q.front();
    q.pop();
    int y = p.y, x = p.x;
    if (dp[y][x] == k) break;
    for (int i = 0; i < 4; i++) {
      int ny = y + ay[i], nx = x + ax[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w) {
        ans = 1;
        goto END;
      }
      if (a[ny][nx] == '#') continue;
      if (~dp[ny][nx]) continue;
      dp[ny][nx] = dp[y][x] + 1;
      q.emplace(ny, nx);
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (dp[i][j] < 0 || dp[i][j] > k) continue;
      int tmp = (i - 1) / k + 1;
      tmp = min(tmp, (j - 1) / k + 1);
      tmp = min(tmp, ((h - i) - 1) / k + 1);
      tmp = min(tmp, ((w - j) - 1) / k + 1);
      ans = min(ans, tmp + 1);
    }
  }
END:
  cout << ans << endl;
  return 0;
}
