#include <bits/stdc++.h>
using namespace std;
struct Node {
  int x, y;
};
int h, w, k;
char r[810][810];
int s, t;
int d[810][810];
int ans;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
void Bfs() {
  queue<Node> q;
  q.push((Node){s, t});
  d[s][t] = 0;
  while (!q.empty()) {
    Node t = q.front();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int xx = t.x + dx[i], yy = t.y + dy[i];
      if (xx < 1 || xx > h || yy < 1 || yy > w || d[xx][yy] != 0x3f3f3f3f ||
          r[xx][yy] == '#')
        continue;
      d[xx][yy] = d[t.x][t.y] + 1;
    }
  }
}
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 1; i <= h; ++i) {
    scanf("%s", r[i] + 1);
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (r[i][j] == 'S') {
        s = i;
        t = j;
        break;
      }
    }
  }
  memset(d, 0x3f, sizeof(d));
  Bfs();
  ans = 0x3f3f3f3f;
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (d[i][j] > k) continue;
      int dis = 0x3f3f3f3f;
      dis = min(dis, i - 1);
      dis = min(dis, j - 1);
      dis = min(dis, h - i);
      dis = min(dis, h - j);
      ans = min(ans, (dis + k - 1) / k + 1);
    }
  }
  printf("%d\n", ans);
  return 0;
}
