#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
template <typename T, typename U>
istream &operator>>(istream &i, pair<T, U> &A) {
  i >> A.first >> A.second;
  return i;
}
template <typename T>
istream &operator>>(istream &i, vector<T> &A) {
  for (auto &I : A) {
    i >> I;
  }
  return i;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &A) {
  o << A.first << " " << A.second;
  return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<T> &A) {
  int i = A.size();
  for (auto &I : A) {
    o << I << (--i ? " " : "");
  }
  return o;
}
vector<long long int> dx = {1, 0, -1, 0};
vector<long long int> dy = {0, 1, 0, -1};
inline bool valid(int x, int y, int H, int W) {
  return 0 <= x && x < H && 0 <= y && y < W;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long int H, W, K;
  cin >> H >> W >> K;
  vector<string> A(H);
  cin >> A;
  vector<vector<int>> dp1(H, vector<int>(W, MOD));
  queue<pair<long long int, long long int>> Q;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        dp1[i][j] = 0;
        Q.push({i, j});
      }
    }
  }
  while (!Q.empty()) {
    pair<long long int, long long int> u = Q.front();
    Q.pop();
    int x = u.first, y = u.second;
    if (dp1[x][y] == K) {
      continue;
    }
    for (int j = 0; j < 4; j++) {
      int X = x + dx[j];
      int Y = y + dy[j];
      if (valid(X, Y, H, W) && dp1[X][Y] > dp1[x][y] + 1 && A[X][Y] != '#') {
        dp1[X][Y] = dp1[x][y] + 1;
        Q.push({X, Y});
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (dp1[i][j] <= K) {
        dp1[i][j] = 2 * K - 1;
        Q.push({i, j});
      }
    }
  }
  while (!Q.empty()) {
    pair<long long int, long long int> u = Q.front();
    Q.pop();
    int x = u.first, y = u.second;
    if (dp1[x][y] == K) {
      continue;
    }
    for (int j = 0; j < 4; j++) {
      int X = x + dx[j];
      int Y = y + dy[j];
      if (valid(X, Y, H, W) && dp1[X][Y] > dp1[x][y] + 1) {
        dp1[X][Y] = dp1[x][y] + 1;
        Q.push({X, Y});
      }
    }
  }
  long long int ans = MOD;
  for (int i = 0; i < H; i++) {
    ans = min<long long int>(ans, dp1[i][0]);
    ans = min<long long int>(ans, dp1[i].back());
  }
  for (int j = 0; j < W; j++) {
    ans = min<long long int>(ans, dp1[0][j]);
    ans = min<long long int>(ans, dp1.back()[j]);
  }
  ans /= K;
  cout << ans << endl;
  return 0;
}
