#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}
vector<vector<int>> C;
int H, W, K;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
void dfs(int h, int w) {
  for (int i = (int)(0); i < (int)(4); ++i) {
    if (h + dy[i] >= 0 && h + dy[i] < H && w + dx[i] >= 0 && w + dx[i] < W) {
      if (C[h + dy[i]][w + dx[i]] < 0) {
        C[h + dy[i]][w + dx[i]] = C[h][w] + 1;
        dfs(h + dy[i], w + dx[i]);
      }
    }
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> H >> W >> K;
  vector<string> A(H);
  for (int i = (int)(0); i < (int)(H); ++i) cin >> A[i];
  pair<int, int> s;
  for (int i = (int)(0); i < (int)(H); ++i)
    for (int j = (int)(0); j < (int)(W); ++j) {
      if (A[i][j] == 'S') s = make_pair(i, j);
    }
  C.resize(H);
  for (int i = (int)(0); i < (int)(H); ++i) {
    vector<int> c(W);
    for (int j = (int)(0); j < (int)(W); ++j) {
      if (A[i][j] == 'S' || A[i][j] == '.') {
        c[j] = -1;
      } else {
        c[j] = 2000000007;
      }
    }
    C[i] = c;
  }
  C[s.first][s.second] = 0;
  dfs(s.first, s.second);
  int mi = 2000000007;
  for (int i = (int)(0); i < (int)(H); ++i)
    for (int j = (int)(0); j < (int)(W); ++j) {
      if (C[i][j] <= K && C[i][j] >= 0) {
        mi = min(mi, i);
        mi = min(mi, H - 1 - i);
        mi = min(mi, j);
        mi = min(mi, W - 1 - j);
      }
    }
  output(1 + (mi + K - 1) / K);
  return 0;
}
