#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last) out << ",";
  }
  out << "]";
  return out;
}
const int maxw = 801;
int h, w, k;
queue<int> qi, qj;
int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, 0, -1};
bool v[maxw][maxw];
char g[maxw][maxw];
void dfs(int i, int j, int n) {
  for (int k = 0; k < 4; ++k) {
    int ni = i + di[k], nj = j + dj[k];
    if (n != 0 && 0 <= ni && ni < h && 0 <= nj && nj < w && (!v[ni][nj]) &&
        g[ni][nj] != '#') {
      v[ni][nj] = true;
      qi.push(ni);
      qj.push(nj);
      dfs(ni, nj, n - 1);
    }
  }
}
void solve() {
  cin >> h >> w >> k;
  int si, sj;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cin >> g[i][j];
      if (g[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  memset(v, 0, sizeof(v));
  qi = queue<int>();
  qj = queue<int>();
  qi.push(si);
  qj.push(sj);
  v[si][sj] = true;
  dfs(si, sj, k);
  int ans = h + w + 1;
  while (!qi.empty()) {
    int i = qi.front(), j = qj.front();
    qi.pop();
    qj.pop();
    if ((i == 0 && j == 0) || (i == 0 && j == w - 1) ||
        (i == h - 1 && j == 0) || (i == h - 1 && j == w - 1)) {
      cout << 1 << endl;
      return;
    }
    int height = min(abs(h - 1 - i), i);
    int width = min(abs(w - 1 - j), j);
    int d = min(height, width);
    ans = min(ans, (d + k - 1) / k + 1);
  }
  cout << ans << endl;
}
int main() {
  solve();
  return 0;
}
