#include <bits/stdc++.h>
using namespace std;
const int N = 800;
int dx[4] = {-1, 0, 0, +1};
int dy[4] = {0, -1, +1, 0};
string s[N];
int n, m, k;
int ans;
void dfs(int x, int y, int t) {
  if (x < 0 || x >= n || y < 0 || y >= m) return;
  if (s[x][y] == '#') return;
  if (!t) return;
  int cur = min(x, y);
  cur = min(cur, n - 1 - x);
  cur = min(cur, m - 1 - y);
  ans = min(ans, (cur + k - 1) / k + 1);
  for (int i = 0; i < 4; i++) {
    dfs(x + dx[i], y + dy[i], t - 1);
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) cin >> s[i];
  int x, y;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'S') {
        x = i;
        y = j;
      }
  ans = n * m;
  dfs(x, y, k);
  printf("%d\n", ans);
  return 0;
}
