#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
int dp[808][808];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  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 = 800000;
  memset(dp, -1, sizeof(dp));
  queue<pair<int, int> > q;
  q.push({sy, sx});
  dp[sy][sx] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int y = p.first;
    int x = p.second;
    if (y == 0 || y == h - 1 || x == 0 || x == w - 1) {
      cout << 1 << endl;
      return 0;
    }
    if (dp[y][x] == k) break;
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
      if (a[ny][nx] == '#') continue;
      if (~dp[ny][nx]) continue;
      dp[ny][nx] = dp[y][x] + 1;
      q.push({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 - 1) - 2) / k + 1);
      tmp = min(tmp, ((w - j) - 2) / k + 1);
      ans = min(ans, tmp + 1);
    }
  cout << ans << endl;
}
