#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long longinf = 1LL << 60;
const long long mod = 1e9 + 7;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  for (int i = (int)(0); i < (int)(H); i++) cin >> A[i];
  int s = -1;
  for (int i = (int)(0); i < (int)(H); i++)
    for (int j = (int)(0); j < (int)(W); j++)
      if (A[i][j] == 'S') s = i * W + j;
  int dx[] = {0, -1, 0, 1};
  int dy[] = {-1, 0, 1, 0};
  vector<int> dist(H * W);
  vector<int> S;
  auto bfs = [&]() {
    queue<int> q;
    q.push(s);
    vector<int> vis(H * W);
    while (!q.empty()) {
      int cur = q.front();
      q.pop();
      if (vis[cur]) continue;
      S.push_back(cur);
      vis[cur] = true;
      int x = cur / W;
      int y = cur % W;
      for (int k = (int)(0); k < (int)(4); k++) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        int ne = nx * W + ny;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
        if (vis[ne]) continue;
        if (A[nx][ny] == '#') continue;
        if (dist[cur] + 1 > K) continue;
        dist[ne] = dist[cur] + 1;
        q.push(ne);
      }
    }
  };
  bfs();
  int ans = inf;
  for (int s : S) {
    int x = s / W;
    int y = s / W;
    ans = min((min({x, y, H - 1 - x, W - 1 - y}) + K - 1) / K, ans);
  }
  cout << ans + 1 << endl;
  return 0;
}
