#include <bits/stdc++.h>
using namespace std;
class Node {
 public:
  int x, y, d;
  Node(int a, int b, int c) {
    x = a;
    y = b;
    d = c;
  }
};
bool operator>(const Node &lhs, const Node &rhs) { return lhs.d > rhs.d; }
bool operator<(const Node &lhs, const Node &rhs) { return lhs.d < rhs.d; }
int f(int x, int y, int w, int h, int k) {
  int ww = ((x) > (w - x - 1)) ? (w - x - 1) : (x);
  int hh = ((y) > (h - y - 1)) ? (h - y - 1) : (y);
  ww = (ww + k - 1) / k;
  hh = (hh + k - 1) / k;
  return (ww > hh) ? hh : ww;
}
int main() {
  int hx[4] = {0, 0, 1, -1};
  int hy[4] = {-1, 1, 0, 0};
  vector<string> s;
  int w, h, k;
  cin >> h >> w >> k;
  s.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int x, y;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        s[i][j] = '#';
        x = j;
        y = i;
      }
    }
  }
  multiset<Node> q;
  Node node(x, y, 1);
  int ans = f(x, y, w, h, k) + 1;
  q.insert(node);
  while (q.size() > 0) {
    Node now = *q.begin();
    q.erase(q.begin());
    if (now.x < 0 || now.x >= w || now.y < 0 || now.y >= h) {
      ans = 0;
      break;
    }
    if (f(now.x, now.y, w, h, k) + 1 < ans) {
      ans = f(now.x, now.y, w, h, k) + 1;
    }
    if (now.d == k) {
      continue;
    }
    for (int i = 0; i < 4; i++) {
      if (s[now.y + hy[i]][now.x + hx[i]] == '.') {
        if (now.x + hx[i] == 0 || now.x + hx[i] == w - 1 ||
            now.y + hy[i] == 0 || now.y + hy[i] == h - 1) {
          ans = 1;
          continue;
        }
        s[now.y + hy[i]][now.x + hx[i]] = '#';
        Node next(now.x + hx[i], now.y + hy[i], now.d + 1);
        q.insert(next);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
