#include <bits/stdc++.h>
using namespace std;
int read() {
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
long long READ() {
  long long xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
char one() {
  char ch = getchar();
  while (ch == ' ' || ch == '\n') ch = getchar();
  return ch;
}
const int maxn = 810;
const int ws_[4] = {1, 0, -1, 0}, ad_[4] = {0, 1, 0, -1};
int N, M, K, sx, sy, ans;
char mp[maxn][maxn];
bool vis[maxn][maxn];
struct my {
  int x, y, v;
  my(int x = 0, int y = 0, int v = 0) { this->x = x, this->y = y, this->v = v; }
};
inline bool out(int x, int y) { return x < 1 || y < 1 || x > N || y > M; }
inline int dis(int x, int y) {
  int d = min(min(x, y), min(N - x, M - y));
  return d / K + (d % K != 0);
}
queue<my> q;
void bfs() {
  q.push(my(sx, sy, 0));
  vis[sx][sy] = 1;
  ans = 1 << 30;
  while (!q.empty()) {
    my tmp = q.front();
    q.pop();
    ans = min(ans, dis(tmp.x, tmp.y) + 1);
    if (tmp.v < K)
      for (int i = 0; i < 4; i++) {
        int tx = tmp.x + ws_[i], ty = tmp.y + ad_[i], tv = tmp.v + 1;
        if (!out(tx, ty) && mp[tx][ty] == '.' && !vis[tx][ty]) {
          vis[tx][ty] = 1;
          q.push(my(tx, ty, tv));
        }
      }
  }
}
int main() {
  N = read(), M = read(), K = read();
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++) {
      mp[i][j] = one();
      if (mp[i][j] == 'S') sx = i, sy = j;
    }
  bfs();
  printf("%d\n", ans);
  return 0;
}
