#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> field(H);
  for (int i = 0; i < H; i++) {
    cin >> field[i];
  }
  int si = -1, sj = -1;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (field[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  queue<vector<int>> que;
  que.emplace(vector<int>{si, sj, 0});
  vector<vector<bool>> visited(H, vector<bool>(W, false));
  visited[si][sj] = true;
  const int dd[] = {0, 1, 0, -1, 0};
  int ans = H * W + 1;
  while (que.size()) {
    auto state = que.front();
    que.pop();
    if (state[0] == 0 || state[0] == H - 1 || state[1] == 0 ||
        state[1] == W - 1) {
      ans = 1;
      break;
    }
    int shortest =
        min(min(state[0], H - 1 - state[0]), min(state[1], W - 1 - state[1]));
    ans = min(ans, 1 + (shortest + K - 1) / K);
    for (int i = 0; i < 4; i++) {
      int ny = state[0] + dd[i], nx = state[1] + dd[i + 1];
      if (ny < 0 || H <= ny || nx < 0 || W <= nx || field[ny][nx] == '#')
        continue;
      if (!visited[ny][nx]) {
        visited[ny][nx] = true;
        que.emplace(vector<int>{ny, nx, state[2] + 1});
      }
    }
  }
  cout << ans << endl;
  return 0;
}
