#include <bits/stdc++.h>
using namespace std;
const int N = 805;
struct node {
  int x, y;
  node() {}
  node(int x, int y) : x(x), y(y) {}
};
int n, m, d, sx, sy, x, y, ans;
int dx[4] = {0, 1, -1, 0}, dy[4] = {1, 0, 0, -1}, dep[N][N];
char s[N][N];
queue<node> q;
int main() {
  int i, j;
  node k;
  scanf("%d%d%d", &n, &m, &d);
  for (i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (j = 1; j <= m; j++)
      if (s[i][j] == 'S') sx = i, sy = j;
  }
  q.push(node(sx, sy));
  ans = n + m;
  while (!q.empty()) {
    k = q.front();
    q.pop();
    x = k.x;
    y = k.y;
    ans = min(ans, min(min(x - 1, n - x), min(y - 1, n - y)));
    if (dep[x][y] == d) continue;
    for (i = 0; i < 4; i++) {
      x = k.x + dx[i];
      y = k.y + dy[i];
      if (x < 1 || x > n || y < 1 || y > m) continue;
      if (dep[x][y] || s[x][y] != '.') continue;
      dep[x][y] = dep[k.x][k.y] + 1;
      q.push(node(x, y));
    }
  }
  printf("%d\n", (ans + d - 1) / d + 1);
  return 0;
}
