//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x,a) for (const auto& x: a)
#define sz(x) (int)(x).size()
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int _i = 0; _i < (n); _i++) ni(a[_i])
#define nal(a, n) for (int _i = 0; _i < (n); _i++) nl(a[_i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e3 + 5;
const double eps = 1e-9;
char a[N][N];
int h, w, k, dp[N][N];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int in(int x, int y) {
  return 1 <= x && x <= h && 1 <= y && y <= w && a[x][y] != '#';
}

int main() {
  scanf("%d %d %d", &h, &w, &k);
  pii s;
  for (int i = 1; i <= h; i++) {
    scanf("%s", a[i]+1);
    for (int j = 1; j <= w; j++) {
      if (a[i][j] == 'S')
        s = {i, j};
      dp[i][j] = inf;
    }
  }
  deque<pii> pq;
  pq.pb(s);
  dp[s.st][s.nd] = 0;
  while (!pq.empty()) {
    auto it = pq.front(); pq.pop_front();
    if (dp[it.st][it.nd] == k)
      continue;
    for (int i = 0; i < 4; i++) {
      int nx = it.st + dx[i], ny = it.nd + dy[i];
      if (!in(nx, ny) || dp[nx][ny] <= dp[it.st][it.nd] + 1)
        continue;
      dp[nx][ny] = dp[it.st][it.nd] + 1;
      pq.pb({nx, ny});
    }
  }
  int ans = inf;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) if (dp[i][j] != inf) {
      ans = min(ans, (min({i-1, j-1, h-i, w-j}) + k - 1) / k + (dp[i][j]+k-1) / k);
    }
  printf("%d\n", ans);
  return 0;
}
