#include <bits/stdc++.h>
using namespace std;
long long MOD = (long long)1000000000 + 7;
const long long INF = 1e17;
const double pi = acos(-1);
const double EPS = 1e-10;
const long long MAX = 200020;
long long dx[4] = {0, 0, 1, -1};
long long dy[4] = {1, -1, 0, 0};
bool visited[1000][1000];
char A[1000][1000];
long long dist[1000][1000];
long long H, W, K;
long long bfs(long long X, long long Y, long long n) {
  queue<pair<long long, long long> > que;
  for (long long i = 0; i < 1000; i++) {
    for (long long j = 0; j < 1000; j++) {
      dist[i][j] = INF;
    }
  }
  long long res = min({X, H - X - 1, Y, W - Y - 1});
  que.push({X, Y});
  dist[X][Y] = 0;
  visited[X][Y] = true;
  long long cnt = 0;
  while (!que.empty()) {
    if (cnt == n + 1) break;
    cnt++;
    long long x = que.front().first, y = que.front().second;
    que.pop();
    visited[x][y] = true;
    for (long long i = 0; i < 4; i++) {
      long long nx = x + dx[i];
      long long ny = y + dy[i];
      if (nx >= 0 && nx < H && ny >= 0 && ny < W) {
        if (visited[nx][ny]) continue;
        if (A[nx][ny] != '#') {
          dist[nx][ny] = min(dist[nx][ny], dist[x][y] + 1);
          if (dist[nx][ny] <= n) {
            res = min(res, min({nx, H - nx - 1, ny, W - ny - 1}));
            que.push({nx, ny});
          }
        }
      }
    }
  }
  return res;
}
long long dfs(long long x, long long y, long long n) {
  visited[x][y] = true;
  if (n == 0) {
    return min({x, H - x - 1, y, W - y - 1});
  }
  long long res = min({x, H - x - 1, y, W - y - 1});
  for (long long i = 0; i < 4; i++) {
    long long nx = x + dx[i];
    long long ny = y + dy[i];
    if (nx >= 0 && nx < H && ny >= 0 && ny < W) {
      if (visited[nx][ny]) continue;
      if (A[nx][ny] != '#') {
        res = min(res, dfs(nx, ny, n - 1));
      }
    }
  }
  return res;
}
void solve() {
  cin >> H >> W >> K;
  long long sx = 0, sy = 0;
  for (long long i = 0; i < H; i++) {
    for (long long j = 0; j < W; j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') {
        sx = i, sy = j;
      }
    }
  }
  long long rem = bfs(sx, sy, K);
  long long ans = 1;
  ans += (rem + K - 1) / K;
  cout << ans << endl;
  ;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}
