#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy;
  vector<vector<char>> a(h, vector<char>(w));
  vector<vector<bool>> visited(h, vector<bool>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  queue<pair<pair<int, int>, int>> q;
  q.push({{sx, sy}, k});
  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};
  int ans = 10000;
  while (q.empty() == false) {
    int x = q.front().first.first;
    int y = q.front().first.second;
    int t = q.front().second;
    q.pop();
    visited[x][y] = true;
    ans = min(ans, (min({x, y, (h - 1 - x), (w - 1 - y)}) + (k - 1)) / k);
    for (int i = 0; i < 4; i++) {
      if (0 <= x + dx[i] && x + dx[i] < h && 0 <= y + dy[i] && y + dy[i] < w &&
          visited[x + dx[i]][y + dy[i]] == false &&
          a[x + dx[i]][y + dy[i]] != '#' && t - 1 >= 0) {
        q.push({{x + dx[i], y + dy[i]}, t - 1});
      }
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
