#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e17;
const long long MOD = 1000000007;
const long long MAX = 4000001;
long long max(long long a, long long b) {
  if (a > b) {
    return a;
  }
  return b;
}
long long min(long long a, long long b) {
  if (a > b) {
    return b;
  }
  return a;
}
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  if (a < b) {
    return gcd(b, a);
  }
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
struct edge {
  long long ind;
  long long fr;
  long long to;
  long long d;
};
class mint {
  long long x;

 public:
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint& operator+=(const mint& a) {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator-=(const mint& a) {
    if ((x += MOD - a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator*=(const mint& a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint& a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint& a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint& a) const {
    mint res(*this);
    return res *= a;
  }
  mint operator^(const long long& t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(MOD - 2); }
  mint& operator/=(const mint& a) { return (*this) *= a.inv(); }
  mint operator/(const mint& a) const {
    mint res(*this);
    return res /= a;
  }
  friend ostream& operator<<(ostream& os, const mint& m) {
    os << m.x;
    return os;
  }
};
int main() {
  long long H, W, K;
  cin >> H >> W >> K;
  vector<vector<char>> A(H + 1, vector<char>(W + 1));
  vector<vector<long long>> dis(H + 1, vector<long long>(W + 1, INF));
  queue<long long> qh;
  queue<long long> qw;
  for (long long i = 1; i <= H; i++)
    for (long long j = 1; j <= W; j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') {
        dis[i][j] = 0;
        qh.push(i);
        qw.push(j);
      }
    }
  while (!qh.empty()) {
    long long i = qh.front();
    qh.pop();
    long long j = qw.front();
    qw.pop();
    for (long long k = 0; k < 4; k++) {
      long long x = i;
      long long y = j;
      if (k < 2) {
        x += -1 + 2 * (k % 2);
      } else {
        y += -1 + 2 * (k % 2);
      }
      if (x <= 0 || x > H || y <= 0 || y > H) {
        continue;
      }
      if (A[x][y] == '#') {
        continue;
      }
      if (dis[x][y] != INF) {
        continue;
      }
      dis[x][y] = dis[i][j] + 1;
      qh.push(x);
      qw.push(y);
    }
  }
  long long ans = INF;
  for (long long i = 1; i <= H; i++)
    for (long long j = 1; j <= W; j++) {
      if (dis[i][j] > K) {
        continue;
      }
      long long x = min(i - 1, min(j - 1, min(H - i, W - j)));
      if (x > 0) {
        x = (x - 1) / K + 1;
      }
      ans = min(ans, x + 1);
    }
  cout << ans;
  system("PAUSE");
}
