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

int H, W, K;
#define MAX_H 800
#define MAX_W 800
int SI, SJ;
bool U[MAX_H][MAX_W];
int DI[4] = {-1, 0, 1, 0};
int DJ[4] = {0, -1, 0, 1};
int R = INT_MAX;

int main() {
  cin >> H >> W >> K;
  for(int i = 0; i < H; ++i) for(int j = 0; j < W; ++j) {
    char c; cin >> c;
    U[i][j] = c != '.';
    if(c == 'S') { SI = i; SJ = j; }
  }
  queue<tuple<int, int, int>> q; q.push(make_tuple(SI, SJ, 0));
  while(!q.empty()) {
    auto t = q.front(); q.pop(); int i = get<0>(t), j = get<1>(t), d = get<2>(t);
    R = min({R, i, H - 1 - i, j, W - 1 - j});
    if(d == K) continue;
    for(int x = 0; x < 4; ++x) {
      int di = DI[x], dj = DJ[x];
      if(i + di < 0 || i + di >= H || j + dj < 0 || j + dj >= W) continue;
      if(U[i + di][j + dj]) continue;
      U[i + di][j + dj] = true; q.push(make_tuple(i + di, j + dj, d + 1));
    }
  }
  cout << 1 + (R + K - 1) / K << endl;
  return 0;
}
