#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char mp[1000][1000];
int be1, be2;
int ans = 1e9;
int d1[4] = {0, 1, 0, -1}, d2[4] = {1, 0, -1, 0};
int visit[1000][1000];
void dfs(int a, int b, int len) {
  int t = min(min(a, b), min(h - a, w - b));
  int res;
  if (t)
    res = (t - 1) / k + 1 + 1;
  else
    res = len;
  if (res < ans) ans = res;
  if (len == k) return;
  for (int i = 0; i < 4; i++) {
    int x = a + d1[i], y = b + d2[i];
    if (x < 0 || x >= h || y < 0 || y >= w) continue;
    if (mp[x][y] == '#') continue;
    if (visit[x][y]) continue;
    visit[x][y] = 1;
    dfs(x, y, len + 1);
  }
}
int main() {
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> mp[i][j];
      if (mp[i][j] == 'S') be1 = i, be2 = j;
    }
  visit[be1][be2] = 1;
  dfs(be1, be2, 0);
  cout << ans << endl;
  return 0;
}
