#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int si, sj;
  vector<vector<char>> A(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A.at(i).at(j);
      if (A.at(i).at(j) == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  vector<vector<int>> dist(H, vector<int>(W, -1));
  dist.at(si).at(sj) = 0;
  queue<pair<int, int>> q;
  q.push(make_pair(si, sj));
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop();
    for (int k = 0; k < 4; k++) {
      int ni = i + dx[k];
      int nj = j + dy[k];
      if (ni < 0 || nj < 0 || ni >= H || nj >= W) {
        continue;
      }
      if (A.at(ni).at(nj) == '#') {
        continue;
      }
      if (dist.at(ni).at(nj) == -1) {
        dist.at(ni).at(nj) = dist.at(i).at(j) + 1;
        q.push(make_pair(ni, nj));
      }
    }
  }
  int ans = 10000;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (A.at(i).at(j) == '#') {
        continue;
      }
      if (dist.at(i).at(j) > K) {
        continue;
      }
      int sub = 1;
      if (i == 0 || j == 0 || i == H - 1 || j == W - 1) {
        ans = min(ans, sub);
        continue;
      }
      sub += (min({i, j, H - 1 - i, W - 1 - j}) - 1) / K + 1;
      ans = min(ans, sub);
    }
  }
  cout << ans << endl;
}
