#define ONLINE_JUDGE
// {{{ Includes
#ifndef ONLINE_JUDGE
#pragma clang diagnostic warning "-Wall"
#pragma clang diagnostic warning "-Wextra"
#pragma clang diagnostic warning "-Weverything"
#pragma clang diagnostic warning "-Wconversion"
#pragma clang diagnostic ignored "-Wvla"
#pragma clang diagnostic ignored "-Wvla-extension"
#pragma clang diagnostic ignored "-Wshadow"
#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
#pragma clang diagnostic ignored "-Wunused-macros"
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wsign-conversion"
#pragma clang diagnostic ignored "-Wsign-compare"
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wmissing-variable-declarations"
#pragma clang diagnostic ignored "-Wglobal-constructors"
#pragma clang diagnostic ignored "-Wunused-const-variable"
#pragma clang diagnostic ignored "-Wpadded"
#pragma clang diagnostic ignored "-Wcomma"
#pragma clang diagnostic ignored "-Wfloat-equal" // it's fine
#pragma clang diagnostic ignored "-Wdouble-promotion" // also
#endif

#include <unordered_map>
#include <random>
#include <climits>
#include <array>
#include <fstream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <queue>
#include <cstring>
#include <limits>
#include <chrono>
#include <unistd.h>
using namespace std;
// }}}
// {{{ Utilities
#ifdef ONLINE_JUDGE
static bool debug = false;
#else
static bool debug = true;
#endif
#define db(x) #x << "=" << (x) << " "
#define dprintf debug && printf

#define All(x) x.begin(), x.end()
#define Loop(i, n) for (int i = 0; i < int(n); ++i)
template <typename T, typename Q, typename S>
bool Bounded(const T &x, const Q &a, const S &b) { return a <= x && x <= b; }

typedef long double Real;
const Real pi = 4*atan(Real(1.0));
const Real Inf = numeric_limits<Real>::infinity();
// https://github.com/JuliaLang/julia/blob/master/base/floatfuncs.jl
Real isapprox(Real x, Real y, Real rtol = sqrt(numeric_limits<Real>::epsilon()), Real atol = Real(0.0), bool nans = false) { return x == y || (nans && isnan(x) && isnan(y)) || (isfinite(x) && isfinite(y) && abs(x - y) <= atol + rtol*max(abs(x), abs(y))); }
typedef long long int ll;
typedef vector<Real> Vd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<bool> Vb;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;

template <typename T> int sz(const T &x) { return (int)x.size(); }
template <typename T, typename Q> bool mem(const T &s, const Q &x) { return s.find(x) != s.end(); }
template <typename T> void push(vector<T>& c, const T& e) { c.push_back(e); }
template <typename T> void push(queue<T>& c, const T& e) { c.push(e); }
template <typename T> void push(set<T>& c, const T& e) { c.insert(e); }
template <typename T> T pop(vector<T>& v) { assert(!v.empty()); T u = v.back(); v.pop_back(); return u;}
template <typename T> T pop(queue<T>& v) { assert(!v.empty()); T u = v.front(); v.pop(); return u;}
template <typename T, typename... Args> auto emplace(vector<T>& c, Args&&... args) -> decltype(c.emplace_back(args...)) { c.emplace_back(args...); }
template <typename T, typename... Args> auto emplace(queue<T>& c, Args&&... args) -> decltype(c.emplace(args...)) { c.emplace(args...); }
template <typename T, typename... Args> auto emplace(set<T>& c, Args&&... args) -> decltype(c.emplace(args...)) { c.emplace(args...); }
template <typename T> void umin(T &x, const T &y) { x = min(x, y); }
template <typename T> void umax(T &x, const T &y) { x = max(x, y); }
template <typename T> int sign(const T &x) {
  return x == 0 ? 0 : x > 0 ? 1 : -1;
}
template <typename T> T square(const T &x) { return x * x; }
// }}}
// {{{ IO
template <typename S, typename T>
istream& operator>>(istream& in, pair<S, T>& p) { return in >> p.first >> p.second; }
template <typename T> T read() { T x; cin >> x; return x; }
template <typename T, typename I = typename T::iterator>
void read(T &cnt) { for (auto& x : cnt) cin >> x; }
template <typename S, typename T>
ostream& operator<<(ostream& out, const pair<S, T>& p) {
  return out << "{" << p.first << ", " << p.second << "}";
}
template <typename T> struct show_container {
  const T &container;
  show_container(const T &container_) : container(container_) {}
};
template <typename T>
ostream &operator<<(ostream &o, const show_container<T> &thing) {
  bool first = true;
  o << "{";
  for (const auto &x : thing.container) {
    if (!first)
      o << ", ";
    first = false;
    o << x;
  }
  o << "}";
  return o;
}
template <typename T> show_container<T> show(const T &container) {
  return show_container<T>(container);
}

#define dprintf debug && printf
struct dout_ { } dout;
#ifdef ONLINE_JUDGE
template <typename T> dout_ &operator<<(dout_ &out, const T &thing) { return out; }
dout_ &operator<<(dout_ &out, ostream &(*thing)(ostream &)) { return out; }
#else
template <typename T> dout_ &operator<<(dout_ &out, const T &thing) { if (debug) cerr << thing; return out; }
dout_ &operator<<(dout_ &out, ostream &(*thing)(ostream &)) { if (debug) cerr << thing; return out; }
#endif

struct outboth {
  ostream &a;
  ostream &b;
  outboth(ostream &a_, ostream &b_) : a(a_), b(b_) {}
};
template <typename T> outboth &operator<<(outboth &both, const T &thing) {
  both.a << thing;
  both.b << thing;
  return both;
}
outboth &operator<<(outboth &both, ostream &(*thing)(ostream &)) {
  both.a << thing;
  both.b << thing;
  return both;
}
// }}}
// {{{ Solver

const array<int,4> DI = {{0, -1, 0, 1}}, DJ = {{1, 0, -1, 0}};

#ifdef ONLINE_JUDGE
void solve(ostream& rout)
#else
void solve(outboth rout)
#endif
{

  int M, N, K; cin >> M >> N >> K; if (!cin) return;
  vector<string> A(M); read(A);
  dout << "============================================================" << endl;
  dout << db(K) << endl; for (auto& a : A) dout << a << endl;

  int iStart = -1, jStart = -1;
  Loop(i, M) Loop(j, N) if (A[i][j] == 'S') {
    assert(iStart == -1);
    A[i][j] = '.', iStart = i, jStart = j;
  }

  int ans = INT_MAX;
  vector<Vi> dist(M, Vi(N, INT_MAX));
  queue<Pii> q;
  push(q, {iStart, jStart});
  dist[iStart][jStart] = 0;
  while (!q.empty()) {
    int i, j; tie(i, j) = pop(q);
    dout << db(i) << db(j) << endl;
    if (dist[i][j] > K) break;
    umin(ans, 1 + (min(min(i, M-1-i), min(j, N-1-j)) + K - 1) / K);
    Loop(d, 4) {
      int u = i + DI[d], v = j + DJ[d];
      if (Bounded(u, 0, M-1) && Bounded(v, 0, N-1) && A[u][v] != '#' && dist[u][v] == INT_MAX) {
        assert(A[u][v] == '.');
        dist[u][v] = 1 + dist[i][j];
        push(q, {u, v});
        dout << db(i) << db(j) << db(u) << db(v) << endl;
      }
    }
  }

  rout << ans << endl;
}

// }}}
// {{{ Main

int main() {
  // freopen("C.in", "r", stdin);
  // freopen("C.out", "w", stdout);
  std::ios::sync_with_stdio(false); cin.tie(nullptr);
  solve(cout);
  return 0;
}
// }}}

// clang-format off
// Local variables:
// compile-command: "make C && ./C"
// irony-additional-clang-options: ("-std=c++11")
// End:


Measured memory_bytes: 3456