#include <bits/stdc++.h>
using namespace std;
int X, Y, k;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
bool In(int x, int y) { return x >= 0 and x < X and y >= 0 and y < Y; }
char s[1005][1005];
int sx, sy;
int dist[1005][1005];
struct Node {
  int x, y, dis;
  Node() {}
  Node(int a, int b, int c) {
    x = a;
    y = b;
    dis = c;
  }
};
void Init() {
  scanf("%d%d%d", &X, &Y, &k);
  for (int i = 0; i < X; i++) {
    scanf("%s", s[i]);
  }
  for (int i = 0; i < X; i++) {
    for (int j = 0; j < Y; j++) {
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
        s[i][j] = '.';
      }
    }
  }
}
queue<Node> q;
void Solve() {
  memset(dist, 0x3f, sizeof(dist));
  q.push(Node(sx, sy, 0));
  while (not q.empty()) {
    Node t = q.front();
    q.pop();
    if (dist[t.x][t.y] > t.dis) {
      dist[t.x][t.y] = t.dis;
      for (int i = 0; i < 4; i++) {
        int xx = t.x + dx[i];
        int yy = t.y + dy[i];
        if (not In(xx, yy)) continue;
        if (s[xx][yy] == '#') continue;
        q.push(Node(xx, yy, t.dis + 1));
      }
    }
  }
  int res = 0x3f3f3f3f;
  for (int i = 0; i < X; i++) {
    for (int j = 0; j < Y; j++) {
      if (s[i][j] == '.' and i > 0 and i < X - 1 and j > 0 and j < Y - 1) {
        res = min(res, (dist[i][j] - 1) / k + 1 +
                           (min(min(i, j), min(X - i, Y - j)) - 1) / k + 1);
      } else if (s[i][j] == '.') {
        res = min(res, (dist[i][j] - 1) / k + 1);
      }
    }
  }
  printf("%d\n", res);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Init();
  Solve();
  return 0;
}
