#include <bits/stdc++.h>
using namespace std;
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, -1, 1, 0, -1, 1, -1, 1};
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
bool isprime[1000000 + 5];
void init_prime() {
  fill(isprime, isprime + 1000000 + 5, true);
  isprime[0] = isprime[1] = false;
  for (int i = (0), loop_end_i = (1000000 + 5); i < (loop_end_i); i++) {
    if (isprime[i]) {
      for (int j = i * 2; j < 1000000; j += i) {
        isprime[j] = false;
      }
    }
  }
}
vector<long long> factors(long long n) {
  vector<long long> v;
  for (long long i = (0), loop_end_i = (1000000); i < (loop_end_i); i++) {
    if (!isprime[i]) continue;
    while (n % i == 0) {
      v.push_back(i);
      n /= i;
    }
  }
  if (n > 1) {
    v.push_back(n);
  }
  return v;
}
template <typename T = int>
class bit {
 public:
  T *bit_arr;
  int size;
  bit(int n) {
    size = n;
    bit_arr = new T[n];
    fill(bit_arr, bit_arr + n, 0);
  }
  ~bit() { delete bit_arr; }
  T sum(int a) {
    T ret = 0;
    while (a) {
      ret += bit_arr[a];
      a -= a & -a;
    }
    return ret;
  }
  T sum(int a, int b) { return sum(b) - sum(a); }
  T get(int a) { return sum(a, a + 1); }
  void add(int a, T x) {
    a++;
    while (a < size) {
      bit_arr[a] += x;
      a += a & -a;
    }
  }
  void set(int a, T x) { add(a, x - get(a)); }
};
template <typename T = int, class Compare = less<T> >
class rmq {
 public:
  int size;
  Compare comp;
  T *dat;
  T init;
  rmq(int n, T init_ = INT_MAX) {
    init = init_;
    size = 1;
    while (size < n) size *= 2;
    dat = new T[2 * size - 1];
    fill(dat, dat + (2 * size - 1), init);
  }
  ~rmq() { delete dat; }
  void set(int k, T a) {
    k += size - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2], comp);
    }
  }
  T get(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return init;
    if (a <= l && r <= b)
      return dat[k];
    else {
      T vl = get(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = get(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr, comp);
    }
  }
  T get(int a, int b) { return get(a, b, 0, 0, size); }
};
char c[810][810];
int h, w, k;
bool a[810][810];
int check(int y, int x, int z) {
  if (a[y][x]) return 1000000007;
  a[y][x] = true;
  int dst = min(min(y - 1, h - y), min(x - 1, w - x));
  int ret = (dst + k - 1) / k;
  if (z) {
    for (int i = (0), loop_end_i = (4); i < (loop_end_i); i++) {
      int yy = y + dy[i], xx = x + dx[i];
      if (!(1 <= yy && yy <= h && 1 <= xx && xx <= w)) continue;
      if (c[yy][xx] == '#') continue;
      ((ret) = min((ret), (check(yy, xx, z - 1))));
    }
  }
  return ret;
}
int main() {
  {
    scanf("%d", &(h));
    scanf("%d", &(w));
    scanf("%d", &(k));
  };
  for (int i = (0), loop_end_i = (810); i < (loop_end_i); i++)
    for (int j = (0), loop_end_j = (810); j < (loop_end_j); j++) c[i][j] = '.';
  for (int i = (0), loop_end_i = (810); i < (loop_end_i); i++)
    c[i][810 - 1] = 0;
  for (int i = (0), loop_end_i = (h); i < (loop_end_i); i++) {
    scanf("%s", c[i + 1] + 1);
  }
  int y, x;
  for (int i = (0), loop_end_i = (h); i < (loop_end_i); i++)
    for (int j = (0), loop_end_j = (w); j < (loop_end_j); j++)
      if (c[i][j] == 'S') y = i, x = j;
  for (int i = (0), loop_end_i = (810); i < (loop_end_i); i++)
    for (int j = (0), loop_end_j = (810); j < (loop_end_j); j++)
      a[i][j] = false;
  int ans = check(y, x, k) + 1;
  printf("%d\n", ans);
}
