#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2147483647;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr double HDINF = 50000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
template <typename T_char>
T_char TL(T_char cX) {
  return tolower(cX);
};
template <typename T_char>
T_char TU(T_char cX) {
  return toupper(cX);
};
const int vy[] = {-1, -1, -1, 0, 1, 1, 1, 0},
          vx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int h, w, k;
string a[1010];
int sx, sy, gx, gy;
int d[1001][1001];
void bfs() {
  queue<pair<int, int>> que;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = HINF;
    }
  }
  que.push(pair<int, int>(sx, sy));
  d[sy][sx] = 0;
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < w && 0 <= ny && ny < h & a[ny][nx] != '#' &&
          d[ny][nx] == HINF) {
        que.push(pair<int, int>(nx, ny));
        d[ny][nx] = d[p.second][p.first] + 1;
      }
    }
  }
}
int main() {
  cin >> h >> w >> k;
  for (LL i = (0); i < (h); i++) cin >> a[i];
  for (LL i = (0); i < (h); i++)
    for (LL j = (0); j < (w); j++) {
      if (a[i][j] == 'S') {
        sx = j, sy = i;
      }
    }
  bfs();
  int ans = INF;
  for (LL i = (0); i < (h); i++) {
    for (LL j = (0); j < (w); j++) {
      if (d[i][j] <= k) {
        int d2 = min({i, w - i - 1, j, h - j - 1});
        ans = min(d2 / k + (d2 % k != 0 ? 1 : 0), ans);
      }
    }
  }
  cout << ans << endl;
}
