#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;
constexpr int H_MAX = 800;
constexpr int W_MAX = 800;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
int H, W, K;
Pii start = {-1, -1};
vector<string> a(H_MAX);
vector<vector<int>> opening_turn(H_MAX, vector<int>(W_MAX, 1 << 30));
vector<vector<int>> shortest_turn(H_MAX, vector<int>(W_MAX, 1 << 30));
inline void calc_opening_turn() {
  priority_queue<pair<int, Pii>, vector<pair<int, Pii>>,
                 greater<pair<int, Pii>>>
      que;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (opening_turn[i][j] == 0) {
        que.emplace(0, Pii(i, j));
      }
    }
  }
  int turn;
  Pii p;
  while (!que.empty()) {
    tie(turn, p) = que.top();
    que.pop();
    if (opening_turn[p.first][p.second] < turn) continue;
    for (int k = 0; k < 4; ++k) {
      Pii q = {p.first + dyx[k][0], p.second + dyx[k][1]};
      if (q.first < 0 || H <= q.first || q.second < 0 || W <= q.second)
        continue;
      if (opening_turn[q.first][q.second] > turn + 1) {
        opening_turn[q.first][q.second] = turn + 1;
        que.emplace(turn + 1, Pii(q.first, q.second));
      }
    }
  }
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      opening_turn[i][j] = (opening_turn[i][j] + K - 1) / K;
    }
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> H >> W >> K;
  for (int i = 0; i < H; ++i) {
    cin >> a[i];
    for (int j = 0; j < W; ++j) {
      if (a[i][j] != '#') {
        if (a[i][j] == 'S') {
          start = {i, j};
        }
        opening_turn[i][j] = 0;
      }
    }
  }
  calc_opening_turn();
  int ans = 1 << 30;
  for (int i = 0; i < H; ++i) {
    ans = min(opening_turn[i][0], ans);
    ans = min(opening_turn[i][W - 1], ans);
  }
  for (int j = 0; j < W; ++j) {
    ans = min(opening_turn[0][j], ans);
    ans = min(opening_turn[H - 1][j], ans);
  }
  cout << ans + 1 << endl;
}
