#include <bits/stdc++.h>
using namespace std;
const int MAXN = 805;
struct point {
  int x, y, d;
};
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int n, m, k;
char str[MAXN][MAXN];
int vis[MAXN][MAXN];
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) cin >> str[i] + 1;
  point rt;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (str[i][j] == 'S') rt = (point){i, j, 0};
  static queue<point> que;
  int ans = INT_MAX;
  auto div_up = [](int a, int b) { return a / b + (a % b != 0); };
  vis[rt.x][rt.y] = 1, que.push(rt);
  while (!que.empty()) {
    point nd = que.front();
    que.pop();
    if (nd.d > k) continue;
    ans = min(
        ans,
        div_up(min(min(nd.x - 1, n - nd.x), min(nd.y - 1, m - nd.y)), k) + 1);
    for (int i = 0; i < 4; i++) {
      int x = nd.x + dx[i], y = nd.y + dy[i];
      if (x < 1 || x > n || y < 1 || y > n || str[x][y] == '#' || vis[x][y])
        continue;
      vis[x][y] = 1;
      que.push((point){x, y, nd.d + 1});
    }
  }
  cout << ans << endl;
  return 0;
}
