#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> rooms(H);
  int sy, sx;
  for (int i = 0; i < (int)(H); ++i) {
    cin >> rooms[i];
    auto idx = rooms[i].find("S");
    if (idx != string::npos) {
      sy = i;
      sx = idx;
    }
  }
  vector<vector<int>> memo(H, vector<int>(W, 999));
  priority_queue<pair<int, pair<int, int>>> Q;
  Q.push({0, {sy, sx}});
  while (!Q.empty()) {
    pair<int, pair<int, int>> q;
    q = Q.top();
    Q.pop();
    if (memo[q.second.first][q.second.first] != 999) continue;
    memo[q.second.first][q.second.first] = -q.first;
    for (int i = 0; i < (int)(4); ++i) {
      int ny = q.second.first + dy[i];
      int nx = q.second.second + dx[i];
      if ((0 <= ny && ny < H && 0 <= nx && nx < W)) {
        if (rooms[ny][nx] == '#') {
          if (memo[ny][nx] == 999) {
            Q.push({-q.first - 1, {ny, nx}});
          }
        } else {
          if (memo[ny][nx] == 999) {
            Q.push({-q.first, {ny, nx}});
          }
        }
      }
    }
  }
  int ans = 1e9;
  for (int i = 0; i < (int)(H); ++i) {
    ans = min(ans, max((memo[i][0] + (K - 1)) / K + 1,
                       (abs(sy - i) + abs(sx - 0) + (K - 1)) / K));
    ans = min(ans, max((memo[i][W - 1] + (K - 1)) / K + 1,
                       (abs(sy - i) + abs(sx - W + 1) + (K - 1)) / K));
  }
  for (int i = 0; i < (int)(W); ++i) {
    ans = min(ans, max((memo[0][i] + (K - 1)) / K + 1,
                       (abs(sy - 0) + abs(sx - i) + (K - 1)) / K));
    ans = min(ans, max((memo[H - 1][i] + (K - 1)) / K + 1,
                       (abs(sy - H + 1) + abs(sx - i) + (K - 1)) / K));
  }
  cout << ans << endl;
  return 0;
}
