#include <bits/stdc++.h>
struct Loc {
  int x;
  int y;
};
const int MAX_SIZE = 900;
int H, W, K;
int d[4][2];
char A[MAX_SIZE][MAX_SIZE];
int dist[MAX_SIZE][MAX_SIZE];
int main(int argc, char **argv) {
  std::cin >> H >> W >> K;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      std::cin >> A[i][j];
    }
  }
  d[0][0] = 1;
  d[0][1] = 0;
  d[1][0] = 0;
  d[1][1] = 1;
  d[2][0] = -1;
  d[2][1] = 0;
  d[3][0] = 0;
  d[3][1] = -1;
  Loc loc_start;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (A[i][j] == 'S') {
        loc_start.x = i;
        loc_start.y = j;
      }
      dist[i][j] = MAX_SIZE * MAX_SIZE;
    }
  }
  std::queue<std::pair<Loc, int>> que;
  que.push(std::pair<Loc, int>({loc_start, 0}));
  while (!que.empty()) {
    std::pair<Loc, int> p = que.front();
    que.pop();
    if (p.second < dist[p.first.x][p.first.y]) {
      dist[p.first.x][p.first.y] = p.second;
      for (int i = 0; i < 4; i++) {
        Loc next = p.first;
        next.x += d[i][0];
        next.y += d[i][1];
        if (1 <= next.x && next.x <= H && 1 <= next.y && next.y <= W &&
            A[next.x][next.y] == '.') {
          que.push(std::pair<Loc, int>({next, p.second + 1}));
        }
      }
    }
  }
  int ret = MAX_SIZE * MAX_SIZE;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (dist[i][j] < MAX_SIZE) {
        int tmp1 = (i - 1) % K == 0 ? (i - 1) / K : (i - 1) / K + 1;
        int tmp2 = (j - 1) % K == 0 ? (j - 1) / K : (j - 1) / K + 1;
        int tmp3 = (H - i) % K == 0 ? (H - i) / K : (H - i) / K + 1;
        int tmp4 = (W - j) % K == 0 ? (W - j) / K : (W - j) / K + 1;
        int tmp = std::min(std::min(tmp1, tmp2), std::min(tmp3, tmp4));
        ret = std::min(ret, tmp + 1);
      }
    }
  }
  std::cout << ret << std::endl;
  return 0;
}
