#include <bits/stdc++.h>
using namespace std;
template <typename T = long long>
inline T IN() {
  T x;
  cin >> x;
  return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
  cin >> head;
  CIN(move(tail)...);
}
template <typename T = string>
inline void eputs(T s) {
  cout << s << "\n";
  exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
template <typename T>
using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using PQ = priority_queue<T>;
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
const double PI = atan(1.0) * 4.0;
const long long LINF = 9e18;
const long long dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
const long long dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
void cinfast() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
string S[800];
long long H, W, sx, sy, gx, gy, K;
vector<vector<long long>> d;
void bfs(pair<long long, long long> s) {
  queue<pair<long long, long long>> q;
  q.emplace(s);
  d.assign(H, vector<long long>(W, LINF));
  d[s.first][s.second] = 0;
  while (!q.empty()) {
    pair<long long, long long> now = q.front();
    q.pop();
    long long y = now.first, x = now.second;
    for (long long i = 0; i < (4); ++i) {
      long long ny = y + dy[2 * i], nx = x + dx[2 * i];
      if (nx < 0 || nx > W - 1 || ny < 0 || ny > H - 1) continue;
      if (S[ny][nx] == '.' && d[ny][nx] == LINF) {
        d[ny][nx] = d[y][x] + 1;
        q.emplace(ny, nx);
      }
    }
  }
}
void bfs2(vector<pair<long long, long long>> s) {
  queue<pair<long long, long long>> q;
  d.assign(H, vector<long long>(W, LINF));
  for (auto &(x) : (s)) {
    d[x.first][x.second] = 0;
    q.emplace(x);
  }
  while (!q.empty()) {
    pair<long long, long long> now = q.front();
    q.pop();
    long long y = now.first, x = now.second;
    for (long long i = 0; i < (4); ++i) {
      long long ny = y + dy[2 * i], nx = x + dx[2 * i];
      if (nx < 0 || nx > W - 1 || ny < 0 || ny > H - 1) continue;
      if (d[ny][nx] == LINF) {
        d[ny][nx] = d[y][x] + 1;
        q.emplace(ny, nx);
      }
    }
  }
}
signed main() {
  cin >> H >> W >> K;
  long long sh, sw;
  for (long long i = 0; i < (H); ++i) {
    cin >> S[i];
    for (long long j = 0; j < (W); ++j)
      if (S[i][j] == 'S') {
        S[i][j] = '.';
        sh = i, sw = j;
      }
  }
  bfs(pair<long long, long long>(sh, sw));
  vector<pair<long long, long long>> start;
  for (long long i = 0; i < (H); ++i)
    for (long long j = 0; j < (W); ++j)
      if (d[i][j] <= K) start.emplace_back(pair<long long, long long>(i, j));
  bfs2(start);
  long long md = LINF;
  for (long long i = 0; i < (W); ++i) md = min({d[0][i], d[H - 1][i], md});
  for (long long i = 0; i < (H); ++i) md = min({d[i][0], d[i][W - 1], md});
  cout << md / K + md % K + 1 << "\n";
}
