#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) {
  return make_pair(l.first - r.first, l.second - r.second);
}
const long long int MOD = 1e9 + 7, INF = 20000;
long long int dp[800][800];
char mp[800][800];
long long int H, W, K, sy, sx;
int main() {
  cin >> H >> W >> K;
  for (long long int i = (0), i_end_ = (H); i < i_end_; i++) {
    for (long long int j = (0), j_end_ = (W); j < j_end_; j++) {
      cin >> mp[i][j];
      if (mp[i][j] == 'S') sy = i, sx = j;
      dp[i][j] = INF;
    }
  }
  queue<pair<long long int, long long int> > que;
  long long int dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};
  dp[sy][sx] = 0;
  que.push({sy, sx});
  while (!que.empty()) {
    pair<long long int, long long int> curr = que.front();
    que.pop();
    for (long long int k = (0), k_end_ = (4); k < k_end_; k++) {
      long long int ny = curr.first + dy[k], nx = curr.second + dx[k];
      if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
      if (mp[ny][nx] == '#') continue;
      if (dp[curr.first][curr.second] + 1 > K) continue;
      if (dp[ny][nx] > dp[curr.first][curr.second] + 1) {
        dp[ny][nx] = dp[curr.first][curr.second] + 1;
        que.push({ny, nx});
      }
    }
  }
  long long int minv = 2000;
  for (long long int i = (0), i_end_ = (H); i < i_end_; i++) {
    for (long long int j = (0), j_end_ = (W); j < j_end_; j++) {
      if (dp[i][j] == INF) continue;
      if (i == 0 || j == 0 || i == H - 1 || j == W - 1) {
        cout << 1 << endl;
        return 0;
      }
      chmin(minv, min(min(i / K + 1, (H - i - 1) / K + 1),
                      min(j / K + 1, (W - j - 1) / K + 1)) +
                      1);
    }
  }
  cout << minv << endl;
}
