#include <bits/stdc++.h>
using namespace std;
int n, m, k, dist[805][805], viz[805][805], answer = 1000000007;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
pair<int, int> stiva[805 * 805], start;
char s[805][805];
inline bool outOfMatrix(pair<int, int> poz) {
  return (poz.first < 1 || poz.first > n || poz.second < 1 || poz.second > m);
}
void bfs(pair<int, int> start) {
  int left = 1, right = 0;
  ;
  stiva[right = 1] = start;
  viz[start.first][start.second] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) dist[i][j] = 1000000007;
  dist[start.first][start.second] = 0;
  while (left <= right) {
    pair<int, int> currPoz = stiva[left];
    for (int dir = 0; dir < 4; dir++) {
      pair<int, int> nextPoz =
          make_pair(currPoz.first + dx[dir], currPoz.second + dy[dir]);
      if (viz[nextPoz.first][nextPoz.second] || outOfMatrix(nextPoz) ||
          s[nextPoz.first][nextPoz.second] == '#')
        continue;
      viz[nextPoz.first][nextPoz.second] = 1;
      dist[nextPoz.first][nextPoz.second] =
          dist[currPoz.first][currPoz.second] + 1;
      stiva[++right] = nextPoz;
    }
    left++;
  }
}
int getCost(int px, int py) {
  int D = min(px - 1, min(py - 1, min(n - px, m - py)));
  return D / k;
}
int main() {
  scanf("%d%d%d\n", &n, &m, &k);
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i][j] == 'S') {
        start = make_pair(i, j);
        break;
      }
  if (start.first == 1 || start.first == n || start.second == 1 ||
      start.second == m) {
    printf("0\n");
    return 0;
  }
  bfs(start);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (dist[i][j] <= k) {
        answer = min(answer, getCost(i, j) + 1);
      }
  printf("%d\n", answer);
  return 0;
}
