#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<lint, lint>;
constexpr int MOD = 1000000007;
constexpr int INF = 2147483647;
void yes(bool expr) { cout << (expr ? "Yes" : "No") << "\n"; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (int i = (0), i_end_ = (H); i < i_end_; i++) cin >> A[i];
  deque<P> que;
  vector<vector<int>> dp(H, vector<int>(W, -1));
  for (int i = (0), i_end_ = (H); i < i_end_; i++)
    for (int j = (0), j_end_ = (W); j < j_end_; j++)
      if (A[i][j] == 'S') {
        que.push_back(make_pair(i, j));
        dp[i][j] = 0;
      }
  vector<int> dx = {-1, 1, 0, 0};
  vector<int> dy = {0, 0, -1, 1};
  while (!que.empty()) {
    P p = que.front();
    if (dp[p.first][p.second] == K) break;
    que.pop_front();
    if (p.first == 0 || p.first == H - 1 || p.second == 0 ||
        p.second == W - 1) {
      cout << 1 << endl;
      return 0;
    }
    for (int k = (0), k_end_ = (4); k < k_end_; k++) {
      if (A[p.first + dx[k]][p.second + dy[k]] != '#') {
        dp[p.first + dx[k]][p.second + dy[k]] = dp[p.first][p.second] + 1;
        que.push_back(make_pair(p.first + dx[k], p.second + dy[k]));
      }
    }
  }
  for (int i = (0), i_end_ = (H); i < i_end_; i++)
    for (int j = (0), j_end_ = (W); j < j_end_; j++)
      if (dp[i][j] >= 0 && A[i][j] != '#') {
        que.push_back(make_pair(i, j));
        dp[i][j] = K;
      }
  while (true) {
    P p = que.front();
    que.pop_front();
    if (p.first == 0 || p.first == H - 1 || p.second == 0 ||
        p.second == W - 1) {
      cout << (dp[p.first][p.second] - 1) / K + 1 << endl;
      return 0;
    }
    for (int k = (0), k_end_ = (4); k < k_end_; k++) {
      dp[p.first + dx[k]][p.second + dy[k]] = dp[p.first][p.second] + 1;
      que.push_back(make_pair(p.first + dx[k], p.second + dy[k]));
    }
  }
}
