#include <bits/stdc++.h>
using namespace std;
char a[800][801];
struct st {
  int a, b, c;
  bool operator<(const st& r) const { return c > r.c; }
};
int d[800][800];
int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
signed main() {
  int h, w, k;
  scanf("%d%d%d", &h, &w, &k);
  int sx, sy;
  for (int i = 0; i < (h); i++) {
    scanf("%s", a[i]);
    for (int j = 0; j < (w); j++) {
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  priority_queue<st> que;
  memset(d, 0x3f, sizeof(d));
  d[sx][sy] = 0;
  que.push({sx, sy, 0});
  while (!que.empty()) {
    st p = que.top();
    que.pop();
    if (d[p.a][p.b] != p.c) continue;
    for (int i = 0; i < (4); i++) {
      int nx = p.a + dx[i], ny = p.b + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w &&
          d[nx][ny] > p.c + (a[nx][ny] == '#')) {
        d[nx][ny] = p.c + (a[nx][ny] == '#');
        que.push({nx, ny, d[nx][ny]});
      }
    }
  }
  int Min = 0x3f3f3f3f;
  for (int i = 0; i < (h); i++)
    for (int j = 0; j < (w); j++) {
      if (i == 0 || j == 0 || i == h - 1 || i == w - 1) Min = min(Min, d[i][j]);
    }
  printf("%d\n", Min / k + (Min % k > 0) + 1);
}
