#include <bits/stdc++.h>
using namespace std;
int H, W, K;
char A[805][805];
int depth[805][805];
bool found[805][805];
void bfs(int posx, int posy);
int main() {
  scanf("%d %d %d", &H, &W, &K);
  int i, j;
  int startx, starty;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      depth[i][j] = 114514;
      scanf(" %c", &A[i][j]);
      if (A[i][j] == 'S') {
        startx = j;
        starty = i;
      }
    }
  }
  bfs(startx, starty);
  int ans = 114514;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      if (found[i][j] && depth[i][j] <= K) {
        int tmp = min(min(min(H - 1 - i, i), W - 1 - j), j);
        if (tmp % K == 0) {
          ans = min(ans, 1 + (tmp / K));
        } else {
          ans = min(ans, 2 + (tmp / K));
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
void bfs(int posx, int posy) {
  queue<pair<int, int> > q;
  q.push(make_pair(posx, posy));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    if (p.first > 0) {
      if (!found[p.first - 1][p.second] && A[p.first - 1][p.second] != '#') {
        q.push(make_pair(p.first - 1, p.second));
        found[p.first - 1][p.second] = true;
        depth[p.first - 1][p.second] = depth[p.first][p.second] + 1;
      }
    }
    if (p.first < H - 1) {
      if (!found[p.first + 1][p.second] && A[p.first + 1][p.second] != '#') {
        q.push(make_pair(p.first + 1, p.second));
        found[p.first + 1][p.second] = true;
        depth[p.first + 1][p.second] = depth[p.first][p.second] + 1;
      }
    }
    if (p.second > 0) {
      if (!found[p.first][p.second - 1] && A[p.first][p.second - 1] != '#') {
        q.push(make_pair(p.first, p.second - 1));
        found[p.first][p.second - 1] = true;
        depth[p.first][p.second - 1] = depth[p.first][p.second] + 1;
      }
    }
    if (p.second < W - 1) {
      if (!found[p.first][p.second + 1] && A[p.first][p.second + 1] != '#') {
        q.push(make_pair(p.first, p.second + 1));
        found[p.first][p.second + 1] = true;
        depth[p.first][p.second + 1] = depth[p.first][p.second] + 1;
      }
    }
  }
}
