#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char a[830][830];
inline int pos(int y, int x) { return y * 1024 + x; }
inline int getx(int p) { return p % 1024; }
inline int gety(int p) { return p / 1024; }
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int dist[830][830];
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 0; i < (int)(h); ++i) scanf("%s", a[i]);
  int sy, sx;
  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;
        a[sy][sx] = '.';
      }
  const int INF = 830252521;
  for (int i = 0; i < (int)(h); ++i)
    for (int j = 0; j < (int)(w); ++j) dist[i][j] = INF;
  queue<pair<int, int> > Q;
  Q.push(pair<int, int>(sy, sx));
  dist[sy][sx] = 0;
  while (!Q.empty()) {
    pair<int, int> P = Q.front();
    Q.pop();
    int y = P.first;
    int x = P.second;
    int d = dist[y][x] + 1;
    for (int d = 0; d < (int)(4); ++d) {
      int ny = y + dy[d];
      int nx = x + dx[d];
      if (ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
      if (a[ny][nx] == '#') continue;
      if (dist[ny][nx] <= d) continue;
      dist[ny][nx] = d;
      if (d < k) Q.push(pair<int, int>(ny, nx));
    }
  }
  int ans = INF;
  for (int i = 0; i < (int)(h); ++i)
    for (int j = 0; j < (int)(w); ++j) {
      if (dist[i][j] == INF) continue;
      ans = min((ans), ((i + k - 1) / k));
      ans = min((ans), ((j + k - 1) / k));
      ans = min((ans), ((h - 1 - i + k - 1) / k));
      ans = min((ans), ((w - 1 - j + k - 1) / k));
    }
  printf("%d\n", ans + 1);
  return 0;
}
