#include <bits/stdc++.h>
struct S002 {
  int n;
  S002& operator>(long& x) {
    n = x;
    return *this;
  }
  S002& operator>(long&& x) {
    n = x;
    return *this;
  }
  S002& operator>=(long& x) {
    if (scanf("%ld", &x) <= 0) exit(0);
    return *this;
  }
  S002& operator>=(double& x) {
    if (scanf("%lf", &x) <= 0) exit(0);
    return *this;
  }
  S002& operator>=(std::string& s) {
    if (not(std::cin >> s)) exit(0);
    return *this;
  }
  template <typename a>
  S002& operator>=(std::vector<a>& v) {
    v.resize(n);
    for (long i = 0; i < n; ++i) {
      *this >= v[i];
    }
    return *this;
  }
  template <typename a, std::size_t s>
  S002& operator>=(std::array<a, s>& x) {
    for (long i = 0; i < (long)s; ++i) {
      *this >= x[i];
    }
    return *this;
  }
};
long dx[] = {1, 0, -1, 0};
long dy[] = {0, 1, 0, -1};
struct Solver {
  long h, w, k;
  std::vector<std::string> xss;
  Solver() {
    S002 reader;
    reader >= h >= w >= k > h >= xss;
  }
  long dist[1024][1024];
  long sx, sy;
  long dfs(long x_, long y_, long d) {
    const long inf = 1000000;
    std::queue<std::pair<long, long>> q;
    q.push(std::make_pair(x_, y_));
    dist[y_][x_] = 0;
    long res = inf;
    while (not q.empty()) {
      std::pair<long, long> temp = q.front();
      q.pop();
      long x = temp.first, y = temp.second;
      long t = std::min(
          std::min(std::min((x + k - 1) / k, ((w - 1) - x + k - 1) / k),
                   (y + k - 1) / k),
          ((h - 1) - y + k - 1) / k);
      res = std::min(res, t);
      for (long i = 0; i < 4; ++i) {
        long nx = x + dx[i];
        long ny = y + dy[i];
        if (not(0 <= nx and nx < w)) continue;
        if (not(0 <= ny and ny < h)) continue;
        if (dist[ny][nx] <= dist[y][x] + 1) continue;
        if (xss[ny][nx] == '#') continue;
        dist[ny][nx] = dist[y][x] + 1;
        if (dist[ny][nx] > d) continue;
        q.push(std::make_pair(nx, ny));
      }
    }
    return res;
  }
  void init() {
    for (long i = 0; i < 1024; ++i) {
      for (long j = 0; j < 1024; ++j) {
        dist[i][j] = 1000000;
      }
    }
    for (long i = 0; i < h; ++i) {
      for (long j = 0; j < w; ++j) {
        if (xss[i][j] == 'S') {
          sy = i;
          sx = j;
        }
      }
    }
  }
  void solve() {
    init();
    long res = dfs(sx, sy, 0);
    printf("%ld\n", res + 1);
  }
  ~Solver() {
    if (false) fprintf(stderr, "--end--\n");
  }
};
int main() {
  for (;;) std::unique_ptr<Solver>(new Solver())->solve();
  return 0;
}
