#include <bits/stdc++.h>
using namespace std;
const int MaxN = 803;
int h, w, k, sx, sy;
bool e[MaxN][MaxN], f[MaxN][MaxN];
struct Statu {
  int x, y, k;
  Statu() {}
  Statu(int X, int Y, int K) : x(X), y(Y), k(K) {}
};
deque<Statu> q;
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= h; j++) {
      register char ch = getchar();
      while (ch == ' ' || ch == '\n') ch = getchar();
      e[i][j] = (ch != '#');
      if (ch == 'S') sx = i, sy = j;
    }
  if (sx == 1 || sx == h || sy == 1 || sy == w) {
    puts("0");
    return 0;
  }
  f[sx][sy] = true;
  q.push_back(Statu(sx, sy, 0));
  while (!q.empty()) {
    int tx = q.front().x, ty = q.front().y, tk = q.front().k;
    q.pop_front();
    if (tk == k) continue;
    if (tx == 1 || tx == h || ty == 1 || ty == w) {
      puts("1");
      return 0;
    }
    const int v[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
    for (int i = 0; i < 4; i++) {
      int vx = tx + v[i][0], vy = ty + v[i][1];
      if (vx < 1 || vx > h || vy < 1 || vy > w) continue;
      if (!f[vx][vy] && e[vx][vy]) {
        f[vx][vy] = true;
        q.push_back(Statu(vx, vy, tk + 1));
      }
    }
  }
  int ans = 0x7fffffff;
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      if (f[i][j]) {
        int dist = min(min(i - 1, j - 1), min(h - i, w - j));
        int r = (dist + k - 1) / k;
        ans = min(r + 1, ans);
      }
  printf("%d\n", ans);
  return 0;
}
