#include <bits/stdc++.h>
using namespace std;
int H, W, K;
int bfs(vector<string>& A, int x, int y, int n) {
  int vx[] = {-1, +1, 0, 0};
  int vy[] = {0, 0, -1, +1};
  queue<tuple<int, int, int>> Q;
  Q.emplace(x, y, n);
  int ans = 1e9;
  while (!Q.empty()) {
    int x, y, n;
    tie(x, y, n) = Q.front();
    Q.pop();
    A[y][x] = '$';
    if (n > 0) {
      for (int i = 0; i < 4; i++) {
        int newx = x + vx[i];
        int newy = y + vy[i];
        if (0 <= newx && newx < W && 0 <= newy && newy < H &&
            A[newy][newx] == '.') {
          Q.emplace(newx, newy, n - 1);
        }
      }
    }
    ans = min({
        ans,
        (abs(x) + K - 1) / K + 1,
        (abs(x - (W - 1)) + K - 1) / K + 1,
        (abs(y) + K - 1) / K + 1,
        (abs(y - (H - 1)) + K - 1) / K + 1,
    });
  }
  return ans;
}
int main() {
  cin >> H >> W >> K;
  vector<string> A(H);
  for (auto& a : A) cin >> a;
  int sx = 0, sy = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        sy = i, sx = j;
      }
    }
  }
  cout << bfs(A, sx, sy, K) << endl;
  return 0;
}
