#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> a(h);
  int sx, sy;
  for (long long i = 0; i < (long long)(h); i++) {
    cin >> a[i];
    for (long long j = 0; j < (long long)(w); j++) {
      if (a[i][j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  int res = 1;
  int dy[] = {0, 0, -1, 1};
  int dx[] = {1, -1, 0, 0};
  vector<vector<int>> d(h, vector<int>(w, INT_MAX));
  queue<pair<int, int>> q;
  q.push(pair<int, int>(sy, sx));
  d[sy][sx] = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (long long i = 0; i < (long long)(4); i++) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];
      if (0 <= ny && ny < h && 0 <= nx && nx < w) {
        if (a[ny][nx] == '.' && d[ny][nx] == INT_MAX) {
          d[ny][nx] = d[p.first][p.second] + 1;
          q.push(pair<int, int>(ny, nx));
        }
      }
    }
  }
  int m = INT_MAX;
  for (long long i = 0; i < (long long)(h); i++) {
    for (long long j = 0; j < (long long)(w); j++) {
      if (d[i][j] != INT_MAX) {
        int temp = min({i, j, h - 1 - i, w - 1 - j});
        chmin(m, temp);
      }
    }
  }
  res += (m + k - 1) / k;
  cout << res << "\n";
  return 0;
}
