#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
const ll inf = (1e18);
const ll mod = 1000000007;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll c, ll d) { return c / GCD(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using pp = pair<P, ll>;
V<int> dy = {-1, 0, 1, 0}, dx = {0, -1, 0, 1};
int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  V<string> s(h);
  for (int i = 0; i < h; i++) cin >> s[i];
  int sy = 0, sx = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') sy = i, sx = j;
    }
  }
  V<V<ll>> d(h, V<ll>(w, inf));
  d[sy][sx] = 1;
  deque<pp> q;
  q.emplace_back(P(sy, sx), k);
  ll ans = inf;
  while (q.size()) {
    int y = q.front().first.first, x = q.front().first.second;
    ll cnt = q.front().second;
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w) {
        chmin(ans, d[y][x]);
        continue;
      }
      if (d[ny][nx] < d[y][x]) continue;
      if ((s[ny][nx] == '#' && d[y][x] == 1) || cnt == 0) {
        if (d[ny][nx] > d[y][x] + 1) {
          d[ny][nx] = d[y][x] + 1;
          q.emplace_back(P(ny, nx), cnt + k - 1);
        }
      } else {
        if (cnt && d[ny][nx] > d[y][x]) {
          d[ny][nx] = d[y][x];
          q.emplace_front(P(ny, nx), cnt - 1);
        }
        if (d[ny][nx] > d[y][x] + 1) {
          d[ny][nx] = d[y][x] + 1;
          q.emplace_back(P(ny, nx), cnt + k - 1);
        }
      }
    }
  }
  cout << ans << "\n";
}
