#include <bits/stdc++.h>
using namespace std;
int H, W, K;
char t[1000][1000];
int d[1000][1000];
int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
int main() {
  cin >> H >> W >> K;
  int si, sj;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      cin >> t[i][j];
      if (t[i][j] == 'S') si = i, sj = j;
    }
  }
  for (int i = 0; i <= H; ++i) t[i][0] = t[i][W + 1] = '.';
  for (int i = 0; i <= W; ++i) t[0][i] = t[H + 1][i] = '.';
  for (int i = 0; i <= H + 1; ++i) {
    for (int j = 0; j <= W + 1; ++j) {
      d[i][j] = -1;
    }
  }
  priority_queue<pair<int, pair<int, int>>> que;
  que.push({K, {si, sj}});
  while (!que.empty()) {
    int res = que.top().first, posi = que.top().second.first,
        posj = que.top().second.second;
    que.pop();
    if (d[posi][posj] >= 0) continue;
    d[posi][posj] = 0;
    if (res == 0) continue;
    for (int i = 0; i < 4; ++i) {
      if (!(0 <= posi + di[i] && posi + di[i] <= H + 1)) continue;
      if (!(0 <= posj + dj[i] && posj + dj[i] <= W + 1)) continue;
      if (t[posi + di[i]][posj + dj[i]] == '.') {
        que.push({res - 1, {posi + di[i], posj + dj[i]}});
      }
    }
  }
  int ans = 1e9;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (d[i][j] != 0) continue;
      ans = min({ans, (i + K) / K, (H - i + K - 1) / K, (j + K) / K,
                 (W - j + K - 1) / K});
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
