#include <bits/stdc++.h>
using namespace std;
int n, m, k, cx, cy, ans = 1e9;
char s[805][805];
int d[805][805];
const int dx[] = {0, 1, -1, 0}, dy[] = {1, 0, 0, -1};
void bfs() {
  queue<pair<int, int> > q;
  q.push(make_pair(cx, cy));
  d[cx][cy] = 0;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (d[nx][ny] <= d[x][y] + 1 || s[nx][ny] == '#') continue;
      d[nx][ny] = d[x][y] + 1;
      q.push(make_pair(nx, ny));
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) {
    scanf("%s", s[i]);
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'S') cx = i, cy = j, s[i][j] = '.';
      d[i][j] = 1e9;
    }
  }
  bfs();
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (d[i][j] <= k)
        ans = min(ans,
                  1 + (min(min(i, n - i - 1), min(j, m - j - 1)) + k - 1) / k);
  printf("%d\n", ans);
  return 0;
}
