#include <bits/stdc++.h>
using namespace std;
int n, m, k, startx, starty, head, tail;
char c[803][803];
int dist[803][803];
int dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    scanf("%s", c[i] + 1);
    for (int j = 1; j <= m; j++) {
      if (c[i][j] == 'S') startx = i, starty = j, c[i][j] = '.';
      dist[i][j] = 1e9;
    }
  }
  queue<pair<int, int> > q;
  dist[startx][starty] = 0;
  q.push(pair<int, int>(startx, starty));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ux = x + dir[i][0], vy = y + dir[i][1];
      if (ux < 1 || vy < 1 || ux > n || vy > m) continue;
      if (c[ux][vy] == '#' || dist[ux][vy] <= dist[x][y]) continue;
      dist[ux][vy] = dist[x][y] + 1;
      q.push(pair<int, int>(ux, vy));
    }
  }
  int res = 1e9;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (dist[i][j] <= k) {
        res = min(res,
                  1 + (min(min(i - 1, n - i), min(j - 1, m - j)) + k - 1) / k);
      }
    }
  cout << res;
  return 0;
}
