#include <bits/stdc++.h>
using namespace std;
string A[800];
bool reachable_in_one[800][800];
int h_moves[4] = {-1, 1, 0, 0};
int w_moves[4] = {0, 0, -1, 1};
int ans;
void bfs(int s_h, int s_w, int K, int H, int W) {
  queue<pair<int, pair<int, int> > > q;
  q.push(make_pair(0, make_pair(s_h, s_w)));
  reachable_in_one[s_h][s_w];
  while (!q.empty()) {
    auto tmp = q.front();
    q.pop();
    int cur_dist = tmp.first;
    int cur_h = tmp.second.first;
    int cur_w = tmp.second.second;
    ans = min(ans, min(min(cur_h, H - 1 - cur_h), min(cur_w, W - 1 - cur_w)));
    if (cur_dist < K) {
      for (int i = 0; i < 4; i++) {
        int next_h = cur_h + h_moves[i];
        int next_w = cur_w + w_moves[i];
        if (0 <= next_h && next_h < H && 0 <= next_w && next_w < W &&
            !reachable_in_one[next_h][next_w] && A[next_h][next_w] != '#') {
          q.push(make_pair(cur_dist + 1, make_pair(next_h, next_w)));
          reachable_in_one[cur_h][cur_w] = true;
        }
      }
    }
  }
}
int main() {
  int H;
  int W;
  int K;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    cin >> A[i];
  }
  int s_h;
  int s_w;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        s_h = i;
        s_w = j;
        break;
      }
    }
  }
  ans = INT_MAX;
  bfs(s_h, s_w, K, H, W);
  if (ans % K == 0) {
    cout << ans / K + 1 << endl;
  } else {
    cout << ans / K + 1 + 1 << endl;
  }
  return 0;
}
