#include <bits/stdc++.h>
using namespace std;
const int MAXN = 805;
const int dirx[] = {-1, 0, 1, 0};
const int diry[] = {0, 1, 0, -1};
char mapp[MAXN][MAXN];
bool vis[MAXN][MAXN];
int h, w, k;
struct node {
  int x, y, z;
};
int dis(int pos, int limit) {
  return abs(pos - limit) / k + (abs(pos - limit) % k != 0);
}
void bfs(int x, int y) {
  queue<node> q;
  q.push({x, y, 0});
  int ans = INT_MAX;
  while (q.size()) {
    auto cur = q.front();
    q.pop();
    int x = cur.x, y = cur.y, z = cur.z;
    if (x == 0 || x == h - 1 || y == 0 || y == w - 1) {
      printf("1\n");
      return;
    }
    ans = min(ans, 1 + min(min(dis(x, 0), dis(x, h - 1)),
                           min(dis(y, 0), dis(y, w - 1))));
    if (vis[x][y] || z == k) continue;
    vis[x][y] == true;
    for (int i = 0; i < 4; i++) {
      int xx = x + dirx[i], yy = y + diry[i];
      if (xx < 0 || xx >= h || yy < 0 || yy >= w || mapp[xx][yy] == '#' ||
          vis[xx][yy])
        continue;
      q.push({xx, yy, z + 1});
    }
  }
  printf("%d\n", ans);
  return;
}
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 0; i < h; i++) scanf("%s", mapp[i]);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (mapp[i][j] == 'S') {
        bfs(i, j);
        return 0;
      }
    }
  }
  return 0;
}
