#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
  int H, W, K, sy, sx, ans = 5100000;
  cin >> H >> W >> K;
  int D[H + 2][W + 2];
  char A[H + 2][W + 2];
  bool CAN[H + 2][W + 2];
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      cin >> A[i][j];
      if (A[i][j] == 'S') {
        sy = i;
        sx = j;
      }
      D[i][j] = 5100000;
      CAN[i][j] = false;
    }
  }
  queue<pair<int, int>> Q;
  Q.emplace(sy, sx);
  D[sy][sx] = 0;
  while (!Q.empty()) {
    auto P = Q.front();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      int ny = P.first + dy[i], nx = P.second + dx[i];
      if (nx <= 1 || nx >= W || ny <= 1 || ny >= H || A[ny][nx] == '#')
        continue;
      if (CAN[ny][nx]) continue;
      CAN[ny][nx] = true;
      D[ny][nx] = min(D[ny][nx], D[P.first][P.second] + 1);
      Q.emplace(ny, nx);
    }
  }
  vector<pair<int, int>> L;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (D[i][j] <= K) {
        L.push_back(make_pair(i, j));
      }
    }
  }
  for (int i = 0; i < L.size(); ++i) {
    int X = L[i].first, Y = L[i].second;
    chmin(ans, (X + K - 2) / K);
    chmin(ans, (W - X + K - 1) / K);
    chmin(ans, (Y + K - 2) / K);
    chmin(ans, (H - Y + K - 1) / K);
  }
  cout << ans + 1 << '\n';
  return 0;
}
