#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0}, n, m, k, ans, sx, sy, num, u,
    v;
bool a[810][810], b[810][810];
char ch;
struct Node {
  int x, y, dep;
};
queue<Node> Q;
void Bfs() {
  int x, y, dep;
  while (!Q.empty()) {
    x = Q.front().x;
    y = Q.front().y;
    dep = Q.front().dep;
    Q.pop();
    if (x == 1 || x == n || y == 1 || y == m) {
      ans = 0;
      return;
    }
    num = min(min(n - x, m - y), min(x - 1, y - 1));
    num = (num - 1) / k + 1;
    ans = min(ans, num);
    for (int i = 0; i < 4; i++) {
      u = x + dx[i];
      v = y + dy[i];
      if (!b[u][v] && a[u][v] && dep < k && u >= 1 && u <= n && v >= 1 &&
          v <= m) {
        b[x][y] = 1;
        Q.push((Node){u, v, dep + 1});
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      while ((ch = getchar()) != '#' && ch != '.' && ch != 'S')
        ;
      if (ch == '.')
        a[i][j] = 1;
      else if (ch == 'S') {
        a[i][j] = 1;
        sx = i;
        sy = j;
      }
    }
  }
  if (sx == 1 || sx == n || sy == 1 || sy == m) {
    printf("0\n");
    return 0;
  }
  ans = 2147483647;
  Q.push((Node){sx, sy, 0});
  Bfs();
  printf("%d\n", ans + 1);
}
