#include <bits/stdc++.h>
using namespace std;
int get() {
  char ch;
  while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
    ;
  if (ch == '-') {
    int s = 0;
    while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
    return -s;
  }
  int s = ch - '0';
  while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
  return s;
}
const int N = 805;
const int INF = 1e+9;
int h, w, k;
char s[N][N];
int sx, sy;
int dis[N][N], vis[N][N], tim;
int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
void dfs(int x, int y) {
  tim++;
  for (int i = 0; i <= 3; i++) {
    int x1 = x + fx[i][0], y1 = y + fx[i][1];
    if (x1 < 1 || x1 > h || y1 < 1 || y1 > w || dis[x1][y1] < INF ||
        s[x1][y1] == '#')
      continue;
    dis[x1][y1] = dis[x][y] + 1;
    vis[x1][y1] = tim;
  }
  int nt = tim;
  for (int i = 0; i <= 3; i++) {
    int x1 = x + fx[i][0], y1 = y + fx[i][1];
    if (x1 < 1 || x1 > h || y1 < 1 || y1 > w || vis[x1][y1] != nt) continue;
    dfs(x1, y1);
  }
}
int main() {
  h = get();
  w = get();
  k = get();
  for (int i = 1; i <= h; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= w; j++)
      if (s[i][j] == 'S') sx = i, sy = j;
  }
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) dis[i][j] = INF;
  dis[sx][sy] = 0;
  dfs(sx, sy);
  int ans = INF;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      if (s[i][j] != '#' && dis[i][j] <= k) {
        int tmp = min(min(i - 1, h - i), min(j - 1, w - j));
        if (tmp) tmp = (tmp - 1) / k + 1;
        ans = min(ans, tmp + 1);
      }
  printf("%d\n", ans);
  return 0;
}
