#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 100000000;
int H, W, K;
char A[800][800];
int S_i, S_j;
int G_i, G_j;
int d[800][800];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int bfs() {
  queue<pair<int, int> > que;
  for (int i = 0; i < (int)(H); i++)
    for (int j = 0; j < (int)(W); j++) d[i][j] = INF;
  que.push(pair<int, int>(S_i, S_j));
  d[S_i][S_j] = 0;
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    if (p.first == G_i && p.second == G_j) break;
    for (int i = 0; i < (int)(4); i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < H && 0 <= ny && ny < W && A[nx][ny] != '#' &&
          d[nx][ny] == INF) {
        que.push(pair<int, int>(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return d[G_i][G_j];
}
int main() {
  cin.sync_with_stdio(false);
  cin >> H >> W >> K;
  for (int i = 0; i < (int)(H); i++) {
    for (int j = 0; j < (int)(W); j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') {
        S_i = i;
        S_j = j;
      }
    }
  }
  int min_d = min(min(S_i - 1, H - 1 - S_i), min(S_j - 1, W - 1 - S_j));
  int ans = 0;
  int open = 0;
  G_i = S_i;
  G_j = S_j;
  while (1) {
    for (int i = 0; i < (int)(H); i++) {
      for (int j = 0; j < (int)(W); j++) {
        if (A[i][j] == '.') {
          int min_d2 = min(min(i - 1, H - 1 - i), min(j - 1, W - 1 - j));
          if (min_d > min_d2) {
            min_d = min_d2;
            G_i = i;
            G_j = j;
          }
        }
      }
    }
    if (G_i == 0 || G_j == 0 || G_i == H - 1 || G_j == W - 1) {
      ans += bfs() / K;
      if (bfs() % K) ans++;
      if (ans < open) {
        ans += open - ans;
      }
      cout << ans << endl;
      return 0;
    }
    bool flag[4] = {};
    if (G_i < H - 1 - G_i) {
      if (G_j < W - 1 - G_j) {
        flag[0] = true;
      } else {
        flag[1] = true;
      }
    } else {
      if (G_j < W - 1 - G_j) {
        flag[2] = true;
      } else {
        flag[3] = true;
      }
    }
    int count = 1;
    while (count <= K) {
      if (G_i + count > H - 1 || G_i - count < 0 || G_j + count > W - 1 ||
          G_j - count < 0) {
        break;
      }
      if (flag[0]) {
        if (G_i < G_j) {
          A[G_i - count][G_j] = '.';
        } else {
          A[G_i][G_j - count] = '.';
        }
      } else if (flag[1]) {
        if (G_i < G_j) {
          A[G_i - count][G_j] = '.';
        } else {
          A[G_i][G_j + count] = '.';
        }
      } else if (flag[2]) {
        if (G_i < G_j) {
          A[G_i + count][G_j] = '.';
        } else {
          A[G_i][G_j - count] = '.';
        }
      } else {
        if (G_i < G_j) {
          A[G_i + count][G_j] = '.';
        } else {
          A[G_i][G_j + count] = '.';
        }
      }
      count++;
    }
    open++;
  }
  return 0;
}
