#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
//#include <boost/multiprecision/cpp_int.hpp>
//using multiInt = boost::multiprecision::cpp_int;

using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

constexpr int MOD_TYPE = 1;
constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
constexpr int INF = (int)1e9;
constexpr ll LINF = (ll)4e18;
constexpr ld PI = acos(-1.0);
constexpr ld EPS = 1e-11;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";

#pragma endregion

template <typename T>
struct dijkstra
{
  int V;
  const T INF_d;
  struct edge
  {
    int to;
    T cost;
  };
  vector<vector<edge>> E;
  vector<T> d;
  using pt = pair<T, int>;
  dijkstra(int V_, T INF_d_ = LINF) : V(V_), INF_d(INF_d_)
  {
    E.resize(V);
    d.resize(V);
  }

  void add_E(int a, int b, T c = 1, bool directed = true)
  {
    E[a].emplace_back(edge{b, c});
    if (!directed)
      E[b].emplace_back(edge{a, c});
  }

  void calc(int s)
  {
    priority_queue<pt, vector<pt>, greater<pt>> que;
    fill(d.begin(), d.end(), INF_d);
    que.emplace(0LL, s);
    d[s] = 0;
    while (!que.empty())
    {
      pt p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (auto &&e : E[v])
      {
        if (d[e.to] > d[v] + e.cost)
        {
          d[e.to] = d[v] + e.cost;
          que.emplace(d[e.to], e.to);
        }
      }
    }
  }
};

bool chmin(int &a, int b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}

inline int CEIL(int a, int b)
{
  return (a + b - 1) / b;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(30) << setiosflags(ios::fixed);

  int h, w, k;
  cin >> h >> w >> k;
  auto dist = [&](int i, int j) {
    int res = INF;
    chmin(res, i);
    chmin(res, h - 1 - i);
    chmin(res, j);
    chmin(res, w - 1 - j);
    return res;
  };
  string s[801];
  rep(i, h) cin >> s[i];
  dijkstra<int> ds(h * w, INF);
  rep(i, h) rep(j, w - 1)
  {
    if (s[i][j] == '#' || s[i][j + 1] == '#')
      continue;
    ds.add_E(i * w + j, i * w + j + 1, 1, false);
  }
  rep(i, h - 1) rep(j, w)
  {
    if (s[i][j] == '#' || s[i + 1][j] == '#')
      continue;
    ds.add_E(i * w + j, (i + 1) * w + j, 1, false);
  }
  int s_pos;
  rep(i, h) rep(j, w)
  {
    if (s[i][j] == 'S')
      s_pos = i * w + j;
  }
  ds.calc(s_pos);
  int Min = INF;
  rep(i, h) rep(j, w)
  {

    if (ds.d[i * w + j] > k)
      continue;
    if (i == 0 || i == h - 1 || j == 0 || j == w - 1)
    {
      cout << 1 << endl;
      return 0;
    }
    chmin(Min, CEIL(dist(i, j), k) + 1);
  }
  cout << Min << endl;
  return 0;
}

Measured memory_bytes: 48256