#include <bits/stdc++.h>
using namespace std;
struct state {
  int y, x, d;
};
int H, W, K;
char A[800][800];
int sy, sx;
bool visited[800][800];
int dd[5] = {0, 1, 0, -1, 0};
bool isin(int y, int x) { return 0 <= y && y < H && 0 <= x && x < W; }
bool isgoal(int y, int x) {
  return y == 0 || y == H - 1 || x == 0 || x == W - 1;
}
int divceil(int n, int k) { return n / k + (n % k > 0); }
int main(void) {
  cin >> H >> W >> K;
  for (int i = (0); i < H; i++) {
    cin >> A[i];
    for (int j = (0); j < W; j++)
      if (A[i][j] == 'S') A[i][j] = '.', sy = i, sx = j;
  }
  for (int i = (0); i < H; i++)
    for (int j = (0); j < W; j++) visited[i][j] = false;
  queue<struct state> q1;
  struct state first = {sy, sx, 0};
  q1.push(first);
  vector<struct state> f;
  while (!q1.empty()) {
    struct state c = q1.front();
    q1.pop();
    visited[c.y][c.x] = true;
    f.push_back(c);
    for (int i = (0); i < 4; i++) {
      struct state n = {c.y + dd[i], c.x + dd[i + 1], c.d + 1};
      if (n.d <= K && isin(n.y, n.x) && !visited[n.y][n.x] &&
          A[n.y][n.x] == '.')
        q1.push(n);
    }
  }
  int cnt = 100000000;
  for (int i = (0); i < f.size(); i++) {
    int y = f[i].y, x = f[i].x;
    cnt = min(cnt, divceil(y, K));
    cnt = min(cnt, divceil(x, K));
    cnt = min(cnt, divceil(H - y - 1, K));
    cnt = min(cnt, divceil(W - x - 1, K));
  }
  cout << cnt + 1 << endl;
  return 0;
}
