#include <bits/stdc++.h>
using namespace std;
const int MaxN = 800;
int n, m, k;
int dx[5] = {0, -1, 0, 1, 0}, dy[5] = {0, 0, 1, 0, -1};
int dis[MaxN + 5][MaxN + 5];
char a[MaxN + 5][MaxN + 5];
bool vis[MaxN + 5][MaxN + 5];
struct Node {
  int x, y, t;
} seq[MaxN * MaxN + 5], s;
bool Check(int x, int y) {
  if (x < 1 || x > n) return 0;
  if (y < 1 || y > m) return 0;
  if (vis[x][y]) return 0;
  return 1;
}
void Bfs(Node s) {
  memset(vis, 0, sizeof(vis));
  int head = 1, tail = 0;
  seq[++tail] = s;
  vis[s.x][s.y] = 1;
  s.t = 0;
  while (head <= tail) {
    Node now = seq[head];
    if (now.t == k) break;
    for (int i = 1; i <= 4; i++) {
      Node p;
      p.x = now.x + dx[i];
      p.y = now.y + dy[i];
      p.t = now.t + 1;
      if (Check(p.x, p.y) && a[p.x][p.y] != '#')
        seq[++tail] = p, vis[p.x][p.y] = 1;
    }
    head++;
  }
  memset(dis, 0x3f, sizeof(dis));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (vis[i][j]) dis[i][j] = 0;
  head = 1;
  while (head <= tail) {
    Node now = seq[head];
    for (int i = 1; i <= 4; i++) {
      Node p;
      p.x = now.x + dx[i];
      p.y = now.y + dy[i];
      if (Check(p.x, p.y)) {
        seq[++tail] = p;
        vis[p.x][p.y] = 1;
        dis[p.x][p.y] = dis[now.x][now.y] + 1;
      }
    }
    head++;
  }
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= n; i++)
    ans = min(ans, int(ceil((double)dis[i][m] / (double)k)) + 1);
  for (int j = 1; j <= m; j++)
    ans = min(ans, int(ceil((double)dis[n][j] / (double)k)) + 1);
  printf("%d\n", ans);
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= m; j++) {
      scanf("%c", &a[i][j]);
      if (a[i][j] == 'S') s.x = i, s.y = j;
    }
  }
  Bfs(s);
}
