#include <bits/stdc++.h>
using namespace std;
const int maxn = 810;
const int inf = 0x3fffffff;
struct node {
  int first, second, cnt;
  node() {}
  node(int first, int second, int cnt)
      : first(first), second(second), cnt(cnt) {}
};
int dist[maxn][maxn], sx, sy, w, h, k;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char gra[maxn][maxn];
int bfs() {
  dist[sx][sy] = 0;
  queue<node> q;
  q.push(node(sx, sy, 0));
  while (!q.empty()) {
    node p = q.front();
    q.pop();
    if (p.first == 1 || p.second == 1 || p.second == w || p.first == h)
      return 1;
    for (int i = 0; i < 4; i++) {
      int tx = p.first + dx[i];
      int ty = p.second + dy[i];
      if (tx < 1 || tx > h || ty < 1 || ty > w || gra[tx][ty] == '#' ||
          dist[tx][ty] != -1)
        continue;
      if (dist[tx][ty] == k) continue;
      dist[tx][ty] = p.cnt + 1;
      q.push(node(tx, ty, p.cnt + 1));
    }
  }
  return 0;
}
int main() {
  while (scanf("%d%d%d", &h, &w, &k) != EOF) {
    for (int i = 1; i <= h; i++) {
      char s[maxn];
      scanf("%s", &s[1]);
      for (int j = 1; j <= w; j++) {
        gra[i][j] = s[j];
        if (s[j] == 'S') {
          sx = i;
          sy = j;
        }
      }
    }
    int ans = inf;
    memset(dist, -1, sizeof(dist));
    if (sx == 1 || sy == 1 || sx == h || sy == w) {
      printf("0\n");
      continue;
    }
    int f = bfs();
    if (f == 1) {
      printf("1\n");
      continue;
    }
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (dist[i][j] == -1) continue;
        int d = min(min(i - 1, h - i), min(j - 1, w - j));
        ans = min(ans, (d + k - 1) / k + 1);
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
