#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const long long INFF = 1e18;
int H, W, K;
string A[810];
int memo[810][810];
int sy, sx;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
void solve() {
  for (int y = 0; y < (H); y++)
    for (int x = 0; x < (W); x++) {
      if (memo[y][x] != 0) {
        for (int k = 0; k < (4); k++) {
          int ny = y + dy[k], nx = x + dx[k];
          if (!(0 <= ny && ny < H && 0 <= nx && nx < W)) continue;
          if (A[ny][nx] != '#') continue;
          for (int l = (1); l < (K + 1); l++) {
            int nny = y + l * dy[k], nnx = x + l * dx[k];
            if (!(0 <= nny && nny < H && 0 <= nnx && nnx < W)) continue;
            A[nny][nnx] = '.';
          }
        }
      }
    }
}
int main(void) {
  cin >> H >> W >> K;
  for (int i = 0; i < (H); i++) cin >> A[i];
  for (int i = 0; i < (H); i++)
    for (int j = 0; j < (W); j++)
      if (A[i][j] == 'S') sy = i, sx = j;
  int ans = 0;
  memo[sy][sx] = 1;
  while (1) {
    ans++;
    queue<tuple<int, int, int>> q;
    for (int i = 0; i < (H); i++)
      for (int j = 0; j < (W); j++)
        if (memo[i][j] != 0) q.push(make_tuple(0, i, j));
    while (!q.empty()) {
      int kaisu, y, x;
      tie(kaisu, y, x) = q.front();
      q.pop();
      if (kaisu < K) {
        for (int k = 0; k < (4); k++) {
          int ny = y + dy[k], nx = x + dx[k];
          if (!(0 <= ny && ny < H && 0 <= nx && nx < W)) continue;
          if (memo[ny][nx] != 0) continue;
          if (A[ny][nx] != '.') continue;
          q.push(make_tuple(kaisu + 1, ny, nx));
          memo[ny][nx] = 1;
          if (ny == 0 || nx == 0 || ny == H - 1 || nx == W - 1) {
            printf("%d\n", ans);
            return 0;
          }
        }
      } else {
        break;
      }
    }
    solve();
  }
  return 0;
}
