#include <bits/stdc++.h>
using namespace std;
char str[805][805];
struct Node {
  int x, y, num;
};
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
int h, w, k, ma = 1e9 + 10;
bool vis[805][805];
void d(int a, int b) {
  Node aa, bb;
  aa.x = a, aa.y = b, aa.num = 0;
  vis[a][b] = true;
  queue<Node> q;
  q.push(aa);
  while (!q.empty()) {
    bb = q.front();
    q.pop();
    a = bb.x, b = bb.y;
    if (a == 0 || a == h - 1 || b == 0 || b == w - 1) {
      ma = min(ma, (bb.num % k != 0) + bb.num / k);
      continue;
    }
    for (int i = 0; i < 4; ++i) {
      int xx = a + dir[i][0], yy = b + dir[i][1];
      if (xx >= 0 && xx < h && yy >= 0 && yy < w && !vis[xx][yy]) {
        aa.x = xx, aa.y = yy;
        if (str[xx][yy] == '#') {
          int zz = min(min(a, b), min(h - 1 - a, w - 1 - b));
          ma =
              min(ma, bb.num / k + (bb.num % k != 0) + (zz % k != 0) + zz / k) +
              1;
        } else {
          aa.num = bb.num + 1;
          q.push(aa);
        }
        vis[xx][yy] = true;
      }
    }
  }
}
int main() {
  int x, y;
  scanf("%d%d%d", &h, &w, &k);
  bool flag = false;
  for (int i = 0; i < h; ++i) {
    scanf("%s", str[i]);
    if (!flag) {
      for (int j = 0; j < w; ++j) {
        if (str[i][j] == 'S') flag = true, x = i, y = j;
      }
    }
  }
  d(x, y);
  printf("%d\n", ma);
  return 0;
}
