#include <bits/stdc++.h>
using namespace std;
using LONG = long long int;
using ULONG = unsigned long long int;
bool within(int x, int min, int max) { return min <= x && x <= max; }
int main(void) {
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<char>> f(H);
  for (unsigned int i = 0; i < H; ++i) f[i].resize(W);
  int start = 0;
  int start_row = 0, start_col = 0;
  for (unsigned int i = 0; i < H; ++i)
    for (unsigned int j = 0; j < W; ++j) {
      cin >> f[i][j];
      if (f[i][j] == 'S') {
        start_row = i;
        start_col = j;
      }
    }
  int dx[] = {0, 0, 1, -1};
  int dy[] = {1, -1, 0, 0};
  struct info {
    int row, col;
    int depth;
  };
  stack<info> s;
  vector<vector<bool>> v(H);
  for (unsigned int i = 0; i < H; ++i) v[i].resize(W, false);
  v[start_row][start_col] = true;
  function<void(int, int, int)> walk = [&](int row, int col, int depth) {
    if (depth >= K) return;
    for (unsigned int i = 0; i < 4; ++i) {
      int nrow = row + dy[i];
      int ncol = col + dx[i];
      if (!within(nrow, 0, H - 1) || !within(ncol, 0, W - 1)) continue;
      if (!v[nrow][ncol] && f[nrow][ncol] == '.') {
        v[nrow][ncol] = true;
        walk(nrow, ncol, depth + 1);
      }
    }
  };
  walk(start_row, start_col, 0);
  int ans = 800 * 800 * 2;
  for (unsigned int i = 0; i < H; ++i)
    for (unsigned int j = 0; j < W; ++j) {
      if (!v[i][j]) continue;
      ans = min(ans, (abs((int)j) + K - 1) / K + 1);
      ans = min(ans, (abs((int)i) + K - 1) / K + 1);
      ans = min(ans, (abs((int)j - W - 1) + K - 1) / K + 1);
      ans = min(ans, (abs((int)i - H - 1) + K - 1) / K + 1);
    }
  cout << ans << endl;
  return 0;
}
