#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const double PI = 3.141592653589793238;
const double EPS = 1e-10;
string A[800];
int dist[800][800];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int sx, sy;
  for (int i = 0; i < H; i++) {
    cin >> A[i];
    for (int j = 0; j < W; j++) {
      if (A[i][j] == 'S') sx = j, sy = i;
    }
  }
  memset(dist, -1, sizeof(dist));
  dist[sy][sx] = 0;
  queue<pair<int, int> > Q;
  Q.push(pair<int, int>(sx, sy));
  int ans = 1 << 30;
  while (!Q.empty()) {
    pair<int, int> p = Q.front();
    Q.pop();
    int x = p.first, y = p.second;
    if (dist[y][x] > K) break;
    int d = min(x, W - 1 - x);
    d = min(y, H - 1 - y);
    ans = min(ans, 1 + (d + K - 1) / K);
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (0 > nx || nx >= W || 0 > ny || ny >= H || A[ny][nx] == '#' ||
          dist[ny][nx] != -1)
        continue;
      dist[ny][nx] = dist[y][x] + 1;
      Q.push(pair<int, int>(nx, ny));
    }
  }
  cout << ans << endl;
}
