#include <bits/stdc++.h>
using namespace std;
char g[805][805];
int h, w, k, ans = 1000000007;
bool vis[805][805];
void dfs(int u, int v, int t) {
  if (vis[u][v] || g[u][v] == '#' || t > k) return;
  vis[u][v] = 1;
  int d = min(min(u, h - 1 - u), min(v, w - 1 - v));
  if (d == 0)
    ans = 1;
  else
    ans = min(ans, d / k + (d % k != 0) + 1);
  if (u > 0) dfs(u - 1, v, t + 1);
  if (u + 1 < h) dfs(u + 1, v, t + 1);
  if (v > 0) dfs(u, v - 1, t + 1);
  if (v + 1 < w) dfs(u, v + 1, t + 1);
}
int main() {
  int i, j;
  int u, v;
  scanf("%d%d%d", &h, &w, &k);
  for (i = 0; i < h; i++) {
    scanf("%s", g[i]);
    for (j = 0; j < w; j++) {
      if (g[i][j] == 'S') {
        u = i;
        v = j;
      }
    }
  }
  if (u == 0 || v == 0 || u == h - 1 || v == w - 1) {
    printf("0\n");
    return 0;
  }
  dfs(u, v, 0);
  printf("%d\n", ans);
  return 0;
}
