#include<bits/stdc++.h>
using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for(auto& i: A) cin >> i;

  const int dy[] = {0, 0, 1, -1};
  const int dx[] = {1, -1, 0, 0};
  int sy, sx;
  for(auto i=0; i<H; ++i) for(auto j=0; j<W; ++j) if(A[i][j]=='S') sy= i, sx = j;

  auto res = H*W;

  using P = tuple<int, int, int>;
  queue<P> q;
  q.emplace(0, sy, sx);
  while(!q.empty()) {
    int d, y, x;
    tie(d, y, x) = q.front();
    q.pop();
    auto r = min({y, H-1-y, x, W-1-x});
    res = min(res, 1 + (r+K-1)/K);
    if(d == K) continue;
    for(auto i=0; i<4; ++i) {
      auto ny = y + dy[i];
      auto nx = x + dx[i];
      if(ny<0 || H<=ny) continue;
      if(nx<0 || W<=nx) continue;
      if(A[ny][nx]!='.') continue;
      A[ny][nx] = '#';
      q.emplace(d+1, ny, nx);
    }
  }

  cout << res << endl;
}
