#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
int H, W, K;
vector<string> cells;
vector<vector<int>> reach;
const int MAX = 10000;
bool within_board(int w, int h) { return (0 <= w && w < W && 0 <= h && h < H); }
void debug_print_reach() {
  cout << endl;
  for (int h = 0; h < (int)(H); ++h) {
    for (int w = 0; w < (int)(W); ++w) {
      cout << reach[h][w] << "\t";
    }
    cout << endl;
  }
  cout << endl;
}
int dh[4] = {1, -1, 0, 0};
int dw[4] = {0, 0, 1, -1};
void fill_room(P start, int turn) {
  set<P> poses{start};
  for (int k = 0; k < (int)(K); ++k) {
    set<P> new_poses;
    for (auto& pos : poses) {
      auto w = pos.first;
      auto h = pos.second;
      new_poses.insert(P(w, h));
      for (int d = 0; d < (int)(4); ++d) {
        auto w2 = w + dw[d];
        auto h2 = h + dh[d];
        if (within_board(w2, h2) && cells[h2][w2] != '#' && reach[h2][w2] > k) {
          new_poses.insert(P(w2, h2));
          reach[h2][w2] = k;
        }
      }
    }
    poses = new_poses;
  }
}
int main(void) {
  cin.sync_with_stdio(false);
  cin >> H >> W >> K;
  cells.resize(H);
  reach.resize(H);
  P start;
  for (int h = 0; h < (int)(H); ++h) {
    cin >> cells[h];
    reach[h].resize(W, MAX);
    auto loc = cells[h].find('S');
    if (loc != string::npos) {
      start = make_pair(loc, h);
      reach[h][loc] = 0;
      cells[h][loc] = '.';
    }
  }
  fill_room(start, 0);
  int min_dist = 1e9;
  for (int h = 0; h < (int)(H); ++h) {
    for (int w = 0; w < (int)(W); ++w) {
      if (reach[h][w] == 0) {
        auto dist = min(w, min(h, min(W - 1 - w, H - 1 - h)));
        min_dist = min(min_dist, dist);
      }
    }
  }
  cout << ((min_dist + K - 1) / K) + 1 << endl;
  return 0;
}
