#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;
int IT_MAX = 1 << 17;
const long long MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x1f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const double ERR = 1e-10;
char in[805][805];
int dis[805][805];
int main() {
  int H, W, K, i, j;
  scanf("%d %d %d", &H, &W, &K);
  for (i = 0; i < H; i++) scanf("%s", in[i]);
  int sx, sy;
  for (i = 0; i < H; i++)
    for (j = 0; j < W; j++)
      if (in[i][j] == 'S') sx = i, sy = j;
  memset(dis, 0x3f, sizeof(dis));
  dis[sx][sy] = 0;
  vector<pair<int, int> > Vu;
  Vu.emplace_back(sx, sy);
  for (i = 0; i < Vu.size(); i++) {
    for (j = 0; j < 4; j++) {
      int x = Vu[i].first + "1102"[j] - '1';
      int y = Vu[i].second + "0211"[j] - '1';
      if (x >= 0 && x < H && y >= 0 && y < W && dis[x][y] == INF &&
          in[x][y] != '#') {
        dis[x][y] = dis[sx][sy] + 1;
        Vu.emplace_back(x, y);
      }
    }
  }
  int ans = INF;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      if (dis[i][j] > K) continue;
      int t = min((i + K - 1) / K, (H - 1 - i + K - 1) / K);
      t = min(t, min((j + K - 1) / K, (W - 1 - j + K - 1) / K));
      ans = min(ans, 1 + t);
    }
  }
  return !printf("%d\n", ans);
}
