#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
const double PI = acos(-1);
const double EPS = 1e-14;
template <class T>
using vec = vector<T>;
using ll = long long;
using ull = unsigned long long;
using vi = vec<int>;
using vl = vec<ll>;
using vd = vec<double>;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using vec2 = complex<double>;
template <class T = int>
inline T in() {
  T x;
  cin >> x;
  return x;
}
template <class T = int>
inline vec<T> in(int n) {
  vec<T> v(n);
  for (int i = (int)(0); i < (int)(n); ++i) v[i] = in<T>();
  return v;
}
template <class T = int>
inline void in(T* x, int n) {
  for (int i = (int)(0); i < (int)(n); ++i) x[i] = in<T>();
}
template <class T>
inline void print(const T& x) {
  cout << x << '\n';
}
template <class T>
inline void printd(const T& x, int d) {
  cout << fixed << setprecision(d) << x << '\n';
}
template <class T>
inline void printv(vec<T>& v) {
  int n = (v).size();
  for (int i = (int)(0); i < (int)(n); ++i) cout << v[i] << "\n "[i < n - 1];
}
template <class T>
inline void printv(vec<T>& v, int n) {
  for (int i = (int)(0); i < (int)(n); ++i) cout << v[i] << "\n "[i < n - 1];
}
template <class T>
inline void printa(T* ary, int n) {
  for (int i = (int)(0); i < (int)(n); ++i) cout << ary[i] << "\n "[i < n - 1];
}
int h, w, k;
struct state {
  int cnt;
  int move;
  int blast;
  state() : cnt(INF), move(0), blast(0) {}
  state(int cnt, int move, int blast) : cnt(cnt), move(move), blast(blast) {}
  bool operator<(const state& s) const {
    if (cnt < s.cnt) return true;
    if (cnt == s.cnt && move > s.move) return true;
    if (cnt == s.cnt && move == s.move && blast > s.blast) return true;
    return false;
  }
  state next(char current, char dest) const {
    state ns = *this;
    if (dest == '#') {
      if (current == '.' && blast <= k) ns.move = 0;
      --ns.blast;
      if (ns.blast < 0) {
        ns.move += k;
        ns.blast = k;
        ++ns.cnt;
      }
    }
    --ns.move;
    if (ns.move < 0) {
      ns.move = k;
      ns.blast += k;
      ++ns.cnt;
    }
    return ns;
  }
};
const int DY[] = {-1, 0, 1, 0};
const int DX[] = {0, -1, 0, 1};
vec<string> maze;
state memo[1024][2024];
int bfs(int sy, int sx) {
  queue<pii> que;
  state mini;
  memo[sy][sx] = state(1, k, k);
  for (que.push(pii(sy, sx)); que.size(); que.pop()) {
    pii p = que.front();
    int y = p.first;
    int x = p.second;
    for (int i = (int)(0); i < (int)(4); ++i) {
      int ny = y + DY[i];
      int nx = x + DX[i];
      if (ny < 0 || h <= ny || nx < 0 || w <= nx) continue;
      state ns = memo[y][x].next(maze[y][x], maze[ny][nx]);
      if (ns < memo[ny][nx]) {
        if (ny == 0 || ny == h - 1 || nx == 0 || nx == w - 1) {
          mini = min(mini, ns);
        }
        memo[ny][nx] = ns;
        que.push(pii(ny, nx));
      }
    }
  }
  return mini.cnt;
}
signed main() {
  h = in(), w = in(), k = in();
  maze = in<string>(h);
  int sy, sx;
  for (int i = (int)(0); i < (int)(h); ++i)
    for (int j = (int)(0); j < (int)(w); ++j) {
      if (maze[i][j] == 'S') {
        sy = i;
        sx = j;
        maze[i][j] = '.';
        i = h;
        j = w;
      }
    }
  print(bfs(sy, sx));
  return 0;
}
