#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios {
  fast_ios() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  };
} fast_ios_;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec) is >> v;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "[";
  for (auto v : vec) os << v << ",";
  os << "]";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, set<T> &vec) {
  os << "{";
  for (auto v : vec) os << v << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> &pa) {
  os << "(" << pa.first << "," << pa.second << ")";
  return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, map<TK, TV> &mp) {
  os << "{";
  for (auto v : mp) os << v.first << "=>" << v.second << ",";
  os << "}";
  return os;
}
int H, W, K;
constexpr int Hmax = 800;
vector<vector<int>> A, dp;
int main() {
  cin >> H >> W >> K;
  A.resize(H + 2, vector<int>(W + 2, 1));
  dp = A;
  int sx, sy;
  for (int h = (0), h_end_ = (H); h < h_end_; h++) {
    string s;
    cin >> s;
    for (int w = (0), w_end_ = (W); w < w_end_; w++) {
      if (s[w] != '#') A[h + 1][w + 1] = 0;
      if (s[w] == 'S') sx = h + 1, sy = w + 1;
    }
  }
  dp[sx][sy] = K + 1;
  queue<pint> q;
  q.push(pint(sx, sy));
  int ans = 100000000;
  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.front();
    q.pop();
    int Know = dp[x][y];
    if (!Know) continue;
    ans = min(ans, (x + K - 2) / K);
    ans = min(ans, (H + 1 - x + K - 2) / K);
    ans = min(ans, (y + K - 2) / K);
    ans = min(ans, (W + 1 - y + K - 2) / K);
    if (!A[x + 1][y]) dp[x + 1][y] = Know - 1, q.push(pint(x + 1, y));
    if (!A[x - 1][y]) dp[x - 1][y] = Know - 1, q.push(pint(x - 1, y));
    if (!A[x][y + 1]) dp[x][y + 1] = Know - 1, q.push(pint(x, y + 1));
    if (!A[x][y - 1]) dp[x][y - 1] = Know - 1, q.push(pint(x, y - 1));
  }
  cout << ans + 1 << endl;
}
