#include <bits/stdc++.h>
#define LLI long long int
#define FOR(v, a, b) for(LLI v = (a); v < (b); ++v)
#define FORE(v, a, b) for(LLI v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(LLI v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c,x) ((c).find(x) != (c).end())
#define fst first
#define snd second
#define popcount __builtin_popcount
#define UNIQ(v) (v).erase(unique(ALL(v)), (v).end())
#define bit(i) (1LL<<(i))

#ifdef DEBUG
#include <misc/C++/Debug.cpp>
#else
#define dump(...) ((void)0)
#endif

#define gcd __gcd

using namespace std;
template <class T> constexpr T lcm(T m, T n){return m/gcd(m,n)*n;}

template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;}
template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;}
template <typename T, typename U> bool chmin(T &a, const U &b){return (a>b ? a=b, true : false);}
template <typename T, typename U> bool chmax(T &a, const U &b){return (a<b ? a=b, true : false);}
template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);}

struct Init{
  Init(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(12);
    cerr << fixed << setprecision(12);
  }
}init;


struct Point{
  int x, y;
  Point(): x(0), y(0){}
  Point(int x, int y): x(x), y(y){}
  Point& operator+=(const Point &a){this->x += a.x; this->y += a.y; return *this;}
  Point& operator-=(const Point &a){this->x -= a.x; this->y -= a.y; return *this;}
};

Point operator+(const Point &a, const Point &b){return Point(a.x+b.x, a.y+b.y);}
Point operator-(const Point &a, const Point &b){return Point(a.x-b.x, a.y-b.y);}
bool operator==(const Point &a, const Point &b){return a.x == b.x and a.y == b.y;}
bool operator!=(const Point &a, const Point &b){return !(a == b);}

std::ostream& operator<<(std::ostream &os, const Point &a){
  os << "(" << a.x << "," << a.y << ")";
  return os;
}

namespace grid{
  const std::vector<Point> dir4 = {Point(0,1), Point(0,-1), Point(1,0), Point(-1,0)};
  const std::vector<Point> dir8 = {Point(-1,-1), Point(-1,0), Point(-1,1), Point(1,-1), Point(1,0), Point(1,1), Point(0,-1), Point(0,1)};
}


template <typename Checker>
std::vector<std::vector<int>> bfs_grid(const int H, const int W,
                                       const std::vector<Point> &start_points,
                                       const std::vector<Point> &dir,
                                       const Checker &check_passable)
{
  std::vector<std::vector<int>> dist(H, std::vector<int>(W, -1));
  std::vector<std::vector<bool>> visited(H, std::vector<bool>(W));

  std::queue<Point> q;
  for(auto &p : start_points){
    dist[p.x][p.y] = 0;
    q.push(p);
  }
  
  while(not q.empty()){
    auto cur = q.front(); q.pop();

    if(visited[cur.x][cur.y]) continue;
    visited[cur.x][cur.y] = true;

    for(auto &d : dir){
      auto nxt = cur + d;

      if(nxt.x < 0 or nxt.x >= H or nxt.y < 0 or nxt.y >= W or not check_passable(nxt)) continue;

      if(dist[nxt.x][nxt.y] == -1){
        dist[nxt.x][nxt.y] = dist[cur.x][cur.y] + 1;
        q.push(nxt);
      }else{
        if(dist[nxt.x][nxt.y] > dist[cur.x][cur.y] + 1){
          dist[nxt.x][nxt.y] = dist[cur.x][cur.y] + 1;
          q.push(nxt);
        }
      }
    }
  }

  return dist;
}

template <typename T, typename Func>
vector<Point> find(const vector<T> &a, const Func &f){
  vector<Point> ret;

  int H = a.size();
  int W = a[0].size();

  REP(i,H){
    REP(j,W){
      if(f(a[i][j])){
        ret.emplace_back(i, j);
      }
    }
  }
  
  return ret;
}



int main(){
  int H, W, K;
  while(cin >> H >> W >> K){
    vector<string> A(H); cin >> A;

    auto dist = bfs_grid(H, W, find(A, [](char c){return c == 'S';}), grid::dir4, [&](const auto &p){return A[p.x][p.y] != '#';});

    int ans = INT_MAX;

    REP(i,H){
      REP(j,W){
        if(dist[i][j] >= 0 and dist[i][j] <= K){
          chmin(ans, 1 + (i+K-1) / K);
          chmin(ans, 1 + (j+K-1) / K);
          chmin(ans, 1 + (H-1-i+K-1) / K);
          chmin(ans, 1 + (W-1-j+K-1) / K);
        }
      }
    }

    cout << ans << endl;
  }
  
  return 0;
}


Measured memory_bytes: 3584