#include <bits/stdc++.h>
using namespace std;
vector<vector<char> > A;
vector<vector<int> > dist;
int W, H, K;
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int main() {
  cin >> H >> W >> K;
  A = vector<vector<char> >(H);
  pair<int, int> start;
  for (int i = (int)(0); i < (int)(H); i++) {
    A[i] = vector<char>(W);
    string s;
    cin >> s;
    for (int j = (int)(0); j < (int)(W); j++) {
      A[i][j] = s[j];
      if (A[i][j] == 'S') {
        start = make_pair(i, j);
      }
    }
  }
  dist = vector<vector<int> >(H);
  vector<vector<bool> > visited(H);
  for (int i = (int)(0); i < (int)(H); i++) {
    dist[i] = vector<int>(W, 2000000000);
    visited[i] = vector<bool>(W, false);
  }
  dist[start.first][start.second] = 0;
  visited[start.first][start.second] = true;
  queue<pair<int, int> > q;
  q.push(start);
  while (!q.empty()) {
    pair<int, int> pos = q.front();
    q.pop();
    if (dist[pos.first][pos.second] > K) {
      break;
    }
    for (int d = (int)(0); d < (int)(4); d++) {
      int x = pos.first + dir[d][0];
      int y = pos.second + dir[d][1];
      if (x < 0 || x >= H || y < 0 || y >= W || A[x][y] == '#') continue;
      if (!visited[x][y]) {
        dist[x][y] = dist[pos.first][pos.second] + 1;
        q.push(make_pair(x, y));
      }
    }
  }
  int minimum = 2000000000;
  for (int i = (int)(0); i < (int)(H); i++) {
    for (int j = (int)(0); j < (int)(W); j++) {
      if (dist[i][j] <= K) {
        minimum = min(minimum, min(min(i, H - i), min(j, W - j)));
      }
    }
  }
  cout << (minimum + K - 1) / K + 1 << endl;
  return 0;
}
