#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int MAP[1000][1000];
int x, y, vis[1000][1000];
int ans = 2147483647;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
struct PR {
  int x, y, now;
  PR() { x = y = now = 0; }
  PR(int _x, int _y, int _now) { x = _x, y = _y, now = _now; }
};
bool check(int x, int y) {
  if (x < 0 || x > H) return 0;
  if (y < 0 || y > W) return 0;
  if (MAP[x][y]) return 0;
  return 1;
}
int main() {
  cin >> H >> W >> K;
  for (int i = 1; i <= H; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= W; j++) {
      if (s[j - 1] == '#')
        MAP[i][j] = 1;
      else {
        MAP[i][j] = 0;
        if (s[j - 1] == 'S') x = i, y = j;
      }
    }
  }
  queue<PR> Q;
  Q.push((PR){x, y, 0});
  while (!Q.empty()) {
    PR tmp = Q.front();
    Q.pop();
    if (tmp.now > K) continue;
    if (vis[tmp.x][tmp.y] <= tmp.now && vis[tmp.x][tmp.y] != 0) continue;
    vis[tmp.x][tmp.y] = tmp.now;
    if (tmp.x == 1 || tmp.y == 1 || tmp.x == H || tmp.y == W) ans = min(ans, 1);
    ans = min(ans, min(min((int)ceil((double)(tmp.x - 1) / K),
                           (int)ceil((double)(tmp.y - 1) / K)),
                       min((int)ceil((double)H - tmp.x / K),
                           (int)ceil((double)W - tmp.y / K))) +
                       1);
    for (int i = 0; i < 4; i++) {
      if (check(tmp.x + dx[i], tmp.y + dy[i]))
        Q.push((PR){tmp.x + dx[i], tmp.y + dy[i], tmp.now + 1});
    }
  }
  cout << ans << endl;
  return 0;
}
