#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 100000000;
int H, W, K;
char A[800][800];
int S_i, S_j;
int G_i, G_j;
int d[800][800];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int bfs() {
  queue<pair<int, int> > que;
  for (int i = 0; i < (int)(H); i++)
    for (int j = 0; j < (int)(W); j++) d[i][j] = INF;
  que.push(pair<int, int>(S_i, S_j));
  d[S_i][S_j] = 0;
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    if (p.first == G_i && p.second == G_j) break;
    for (int i = 0; i < (int)(4); i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W && A[nx][ny] != '#' &&
          d[nx][ny] == INF) {
        que.push(pair<int, int>(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[G_i][G_j];
}
int main() {
  cin.sync_with_stdio(false);
  cin >> H >> W >> K;
  for (int i = 0; i < (int)(H); i++) {
    for (int j = 0; j < (int)(W); j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') {
        S_i = i;
        S_j = j;
      }
    }
  }
  int min_d = min(min(S_i - 1, H - 1 - S_i), min(S_j - 1, W - 1 - S_j));
  int ans = 0;
  G_i = S_i;
  G_j = S_j;
  for (int i = 0; i < (int)(H); i++) {
    for (int j = 0; j < (int)(W); j++) {
      if (A[i][j] == '.') {
        int min_d2 = min(min(i - 1, H - 1 - i), min(j - 1, W - 1 - j));
        if (min_d > min_d2) {
          min_d = min_d2;
          G_i = i;
          G_j = j;
        }
      }
    }
  }
  ans += bfs() / K;
  if (bfs() == 0) ans = 1;
  int dis = min(min(G_i, H - 1 - G_i), min(G_j, W - 1 - G_j)) + bfs() % K;
  ans += dis / K;
  if (dis % K) ans++;
  cout << ans << endl;
  return 0;
  return 0;
}
