#include <bits/stdc++.h>
using namespace std;
struct Node {
  int x, y, d;
};
const int INF = 1e9;
char s[805][805];
int used[805][805];
queue<Node> q;
int h, w, k;
bool bfs() {
  int x, y, d;
  while (!q.empty()) {
    x = q.front().x;
    y = q.front().y;
    d = q.front().d;
    q.pop();
    if (x == 1 || x == h || y == 1 || y == w) return true;
    if (d == k) continue;
    if (x > 1 && !used[x - 1][y] && s[x - 1][y] == '.') {
      q.push((Node){x - 1, y, d + 1});
      used[x - 1][y] = 1;
    }
    if (x < h && !used[x + 1][y] && s[x + 1][y] == '.') {
      q.push((Node){x + 1, y, d + 1});
      used[x + 1][y] = 1;
    }
    if (y > 1 && !used[x][y - 1] && s[x][y - 1] == '.') {
      q.push((Node){x, y - 1, d + 1});
      used[x][y - 1] = 1;
    }
    if (y < w && !used[x][y + 1] && s[x][y + 1] == '.') {
      q.push((Node){x, y + 1, d + 1});
      used[x][y + 1] = 1;
    }
  }
}
int main() {
  int stx, sty, flag, ans, minx;
  scanf("%d %d %d", &h, &w, &k);
  for (int i = 1; i <= h; i++) scanf("%s", s[i] + 1);
  flag = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (s[i][j] == 'S') {
        stx = i;
        sty = j;
        flag = 1;
        break;
      }
    }
    if (flag) break;
  }
  memset(used, 0, sizeof(used));
  q.push((Node){stx, sty, 0});
  used[stx][sty] = 1;
  if (bfs()) {
    printf("1\n");
    return 0;
  }
  ans = INF;
  for (int i = 2; i <= h - 1; i++) {
    for (int j = 2; j <= w - 1; j++) {
      if (used[i][j]) {
        minx = min(i - 1, h - i);
        minx = min(minx, j - 1);
        minx = min(minx, w - j);
        ans = min(ans, (minx + k - 1) / k + 1);
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
