#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[4] = {0, 0, 1, -1};
  const int dx[4] = {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 ans = [&](auto y, auto x) {
    auto a = min(abs(y), abs(H-1-y));
    auto b = min(abs(x), abs(W-1-x));
    return 1 + min((a+K-1)/K, (b+K-1)/K);
  };

  auto res = H*W;

  using P = pair<int, int>;
  queue<P> q;
  q.emplace(sy, sx);
  while(!q.empty()) {
    auto y = q.front().first;
    auto x = q.front().second;
    q.pop();
    A[y][x] = '#';
    if(y==0||x==0||y==H-1||x==W-1) res = 0;
    res = min(res, ans(y, x));
    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 || H<=nx) continue;
      if(A[ny][nx]!='.') continue;
      q.emplace(ny, nx);
    }
  }

  cout << res << endl;
}
