#include <bits/stdc++.h>
using namespace std;
static bool debug = false;
template <typename T, typename Q, typename S>
bool Bounded(const T &x, const Q &a, const S &b) {
  return a <= x && x <= b;
}
const long double pi = 4 * atan(long double(1.0));
const long double Inf = numeric_limits<long double>::infinity();
long double isapprox(
    long double x, long double y,
    long double rtol = sqrt(numeric_limits<long double>::epsilon()),
    long double atol = long double(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)));
}
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.back();
  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;
}
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);
}
struct dout_ {
} dout;
template <typename T>
dout_ &operator<<(dout_ &out, const T &thing) {
  return out;
}
dout_ &operator<<(dout_ &out, ostream &(*thing)(ostream &)) { return out; }
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;
}
const array<int, 4> DI = {{0, -1, 0, 1}}, DJ = {{1, 0, -1, 0}};
void solve(ostream &rout) {
  int M, N, K;
  cin >> M >> N >> K;
  if (!cin) return;
  vector<string> A(M);
  read(A);
  dout << "============================================================"
       << endl;
  dout << "K"
       << "=" << (K) << " " << endl;
  for (auto &a : A) dout << a << endl;
  int iStart = -1, jStart = -1;
  for (int i = 0; i < int(M); ++i)
    for (int j = 0; j < int(N); ++j)
      if (A[i][j] == 'S') {
        assert(iStart == -1);
        A[i][j] = '.', iStart = i, jStart = j;
      }
  vector<vector<int> > dist(M, vector<int>(N, INT_MAX));
  queue<pair<int, int> > q;
  push(q, {iStart, jStart});
  dist[iStart][jStart] = 0;
  while (!q.empty()) {
    int i, j;
    tie(i, j) = pop(q);
    dout << "i"
         << "=" << (i) << " "
         << "j"
         << "=" << (j) << " " << endl;
    assert(dist[i][j] < INT_MAX);
    for (int d = 0; d < int(4); ++d) {
      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) {
        dist[u][v] = 1 + dist[i][j];
        push(q, {u, v});
        dout << "i"
             << "=" << (i) << " "
             << "j"
             << "=" << (j) << " "
             << "u"
             << "=" << (u) << " "
             << "v"
             << "=" << (v) << " " << endl;
      }
    }
  }
  int ans = INT_MAX;
  for (int i = 0; i < int(M); ++i)
    for (int j = 0; j < int(N); ++j)
      if (dist[i][j] < INT_MAX) {
        int d = min(min(i, M - 1 - i), min(j, N - 1 - j));
        umin(ans, 1 + (d + 2 * K - 1) / (2 * K));
      }
  rout << ans << endl;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  solve(cout);
  return 0;
}
