#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool visit[810][810];
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  char A[810][810];
  int sx, sy;
  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') {
        sx = j;
        sy = i;
      }
    }
  queue<pair<int, pair<int, int>>> q;
  q.push({0, {sx, sy}});
  int mn = min(abs(sx - 0), abs(W - 1 - sx));
  mn = min(mn, min(abs(sy - 0), abs(H - 1 - sy)));
  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    for (int i = (0); i < (int)4; i++) {
      int nx = now.second.first + dx[i], ny = now.second.second + dy[i];
      int cost = now.first + 1;
      if (!((0) <= (ny) && (ny) < (H)) || !((0) <= (nx) && (nx) < (W)))
        continue;
      if (A[ny][nx] == '#' || visit[ny][nx]) continue;
      visit[ny][nx] = true;
      if (cost % 2 == K % 2) {
        mn = min(mn, min(abs(nx - 0), abs(W - 1 - nx)));
        mn = min(mn, min(abs(ny - 0), abs(H - 1 - ny)));
      }
      if (cost < K) q.push({cost, {nx, ny}});
    }
  }
  int ans = 1;
  while (mn > 0) {
    ans++;
    mn -= K;
  }
  cout << (ans) << endl;
  return 0;
}
