#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (int64_t i = 0; i < H; i++) {
    cin >> A[i];
  }
  int64_t Sx, Sy;
  for (int64_t i = 0; i < H; i++) {
    for (int64_t j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        Sx = j;
        Sy = i;
        i = H;
        j = W;
      }
    }
  }
  int64_t dx[] = {1, 0, -1, 0};
  int64_t dy[] = {0, 1, 0, -1};
  vector<vector<bool>> can_reach(H, vector<bool>(W, false));
  stack<pair<int64_t, int64_t>> st;
  can_reach[Sy][Sx] = true;
  st.push({Sx, Sy});
  while (!st.empty()) {
    pair<int64_t, int64_t> top = st.top();
    st.pop();
    for (int64_t i = 0; i < 4; i++) {
      int64_t nx = top.first + dx[i];
      int64_t ny = top.second + dy[i];
      if (nx < 0 || nx >= W || ny < 0 || ny >= H || can_reach[ny][nx] ||
          A[ny][nx] == '#') {
        continue;
      }
      can_reach[ny][nx] = true;
      st.push({ny, nx});
    }
  }
  int64_t ans = LLONG_MAX;
  for (int64_t i = 0; i < H; i++) {
    for (int64_t j = 0; j < W; j++) {
      if (!can_reach[i][j]) {
        continue;
      }
      int64_t min_distance = LLONG_MAX;
      min_distance = min(min_distance, i);
      min_distance = min(min_distance, j);
      min_distance = min(min_distance, H - 1 - i);
      min_distance = min(min_distance, W - 1 - j);
      if (min_distance == 0) {
        cout << 1 << endl;
        return 0;
      }
      ans = min(ans, 1 + (min_distance + K - 1) / K);
    }
  }
  cout << ans << endl;
}
