#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int h, w, k;
char a[805][805];
queue<pair<int, int> > q;
bool init(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w; }
void dfs(int x, int y, int i) {
  if (i == k) {
    return;
  }
  for (int j = 0; j < 4; j++) {
    if (init(x + dx[i], y + dy[i])) {
      if (a[x + dx[i]][y + dy[i]] == '.') {
        q.push(make_pair(x + dx[i], y + dy[i]));
        dfs(x + dx[i], y + dy[i], i + 1);
      }
    }
  }
}
int main() {
  cin >> h >> w >> k;
  int sx, sy;
  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;
        a[i][j] = '.';
      }
    }
  }
  q.push(make_pair(sx, sy));
  dfs(sx, sy, 0);
  int ans = 1e9;
  while (!q.empty()) {
    ans = min(ans, (min(min(q.front().first, h - q.front().first),
                        min(q.front().second, w - q.front().second)) +
                    k - 1) /
                       k);
    q.pop();
  }
  cout << ans;
  return 0;
}
