#include <bits/stdc++.h>
using namespace std;
char b[810][810];
int d[810][810];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int h, w;
int inb(int x, int y) { return 0 <= x && x < h && 0 <= y && y < w; }
int go(int x, int k) {
  --x;
  return 1 + (x + k - 1) / k;
}
int main() {
  int k;
  int si, sj;
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 1; i <= h; i++) {
    scanf("%s", b[i] + 1);
    for (int j = 1; j <= w; j++) {
      if (b[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  for (int i = 0; i < h + 2; i++) {
    for (int j = 0; j < w + 2; j++) {
      d[i][j] = 10000000;
    }
  }
  d[si][sj] = 0;
  queue<pair<int, int> > q;
  q.push(pair<int, int>(si, sj));
  while (q.size()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    if (x == 1 || x == h || y == 1 || y == w) {
      printf("1\n");
      return 0;
    }
    if (d[x][y] == k) {
      continue;
    }
    for (int z = 0; z < 4; z++) {
      int nx = x + dx[z], ny = y + dy[z];
      if (inb(nx, ny)) {
        if (b[nx][ny] == '.' && d[nx][ny] > 100000) {
          q.push(pair<int, int>(nx, ny));
          d[nx][ny] = d[x][y] + 1;
        }
      }
    }
  }
  int ans = 100000;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (d[i][j] < 100000) {
        int t = 100000;
        t = min(t, go(h + 1 - i, k));
        t = min(t, go(i, k));
        t = min(t, go(w + 1 - j, k));
        t = min(t, go(j, k));
        ans = min(ans, t);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
