#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();
struct Pos {
  ll x, y;
};
void solve() {
  ll H, W, K;
  cin >> H >> W >> K;
  int cell[801][801];
  Pos start;
  for (ll i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (ll j = 0; j < W; j++) {
      if (s[j] == '#') {
        cell[i][j] = 1;
      } else {
        cell[i][j] = 0;
        if (s[j] == 'S') {
          start = {i, j};
        }
      }
    }
  }
  ll dist[801][801];
  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      dist[i][j] = L_INF;
    }
  }
  constexpr Pos step[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
  using Tup = tuple<ll, ll, ll, ll, ll>;
  priority_queue<Tup, vector<Tup>, greater<Tup>> pq;
  pq.emplace(0, 0, 0, start.x, start.y);
  dist[start.x][start.y] = 0;
  while (!pq.empty()) {
    auto [co, co1, co2, x, y] = pq.top();
    pq.pop();
    if (co > dist[x][y]) continue;
    for (ll i = 0; i < 4; i++) {
      ll xn = x + step[i].x;
      ll yn = y + step[i].y;
      if (!(xn >= 0 && xn < H && yn >= 0 && yn < W)) continue;
      ll co1n = co1 + 1;
      ll co2n = co2;
      if (cell[xn][yn] == 1) {
        ++co2n;
      }
      ll con;
      if ((co1n + K - 1) / K >= (co2n + K - 1) / K + 1) {
        con = (co1n + K - 1) / K;
      } else {
        con = (co2n + K - 1) / K + 1;
      }
      if (con >= dist[xn][yn]) continue;
      pq.emplace(con, co1n, co2n, xn, yn);
      dist[xn][yn] = con;
    }
  }
  ll ans = L_INF;
  for (ll i = 0; i < H; i++) {
    ans = min(ans, dist[i][0]);
    ans = min(ans, dist[i][W - 1]);
  }
  for (ll j = 0; j < W; j++) {
    ans = min(ans, dist[0][j]);
    ans = min(ans, dist[H - 1][j]);
  }
  cout << ans << "\n";
}
int main() {
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
  solve();
  cout << flush;
  return 0;
}
