#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<long long, long long>;
const long long INF = 1LL << 60;
long long MOD = 1000000007;
const long double pi = 3.1415926535897932;
using Grid = vector<vector<char>>;
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0) u += m;
  return u;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
vector<long long> divisor(long long n) {
  vector<long long> ret;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) ret.push_back(n / i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}
ll nCr(ll n, ll r) {
  ll ans = 1;
  for (ll i = n; i > n - r; --i) {
    ans = ans * i;
  }
  for (ll i = 1; i < r + 1; ++i) {
    ans = ans / i;
  }
  return ans;
}
map<ll, ll> prime_factor(ll n) {
  map<ll, ll> ret;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1) ret[n] = 1;
  return ret;
}
ll H, W, K;
ll D = INF;
void dfs(ll x, ll y, ll d, Grid& grid, ll check) {
  chmin(D, x);
  chmin(D, W - 1 - x);
  chmin(D, y);
  chmin(D, H - y - 1);
  if (d == K) return;
  if (check == 0) {
    if (grid[x - 1][y] == '.') dfs(x - 1, y, d + 1, grid, 1);
    if (grid[x + 1][y] == '.') dfs(x + 1, y, d + 1, grid, 3);
    if (grid[x][y - 1] == '.') dfs(x, y - 1, d + 1, grid, 4);
    if (grid[x][y + 1] == '.') dfs(x, y + 1, d + 1, grid, 2);
  } else if (check == 1) {
    if (x - 1 >= 0 && grid[x - 1][y] == '.') dfs(x - 1, y, d + 1, grid, check);
    if (y - 1 >= 0 && grid[x][y - 1] == '.') dfs(x, y - 1, d + 1, grid, check);
  } else if (check == 2) {
    if (x - 1 >= 0 && grid[x - 1][y] == '.') dfs(x - 1, y, d + 1, grid, check);
    if (y + 1 < H && grid[x][y + 1] == '.') dfs(x, y + 1, d + 1, grid, check);
  } else if (check == 3) {
    if (x + 1 < W && grid[x + 1][y] == '.') dfs(x + 1, y, d + 1, grid, check);
    if (y + 1 < H && grid[x][y + 1] == '.') dfs(x, y + 1, d + 1, grid, check);
  } else if (check == 4) {
    if (x + 1 < W && grid[x + 1][y] == '.') dfs(x + 1, y, d + 1, grid, check);
    if (y - 1 >= 0 && grid[x][y - 1] == '.') dfs(x, y - 1, d + 1, grid, check);
  }
  return;
}
int main() {
  cin >> H >> W >> K;
  Grid G(W, vector<char>(H));
  ll sx, sy;
  for (ll i = 0; i < (ll)(H); i++) {
    for (ll j = 0; j < (ll)(W); j++) {
      cin >> G[j][i];
      if (G[j][i] == 'S') {
        sx = j;
        sy = i;
      }
    }
  }
  ll check = 0;
  dfs(sx, sy, 0, G, check);
  cout << 1 + (D + K - 1) / K << endl;
}
