#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
  register int c = getchar();
  x = 0;
  int neg = 0;
  for (; ((c < 48 || c > 57) && c != '-'); c = getchar())
    ;
  if (c == '-') {
    neg = 1;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg) x = -x;
}
inline void writeln(int x) {
  char buffor[21];
  register int i = 0;
  int neg = 0;
  if (x < 0) {
    neg = 1;
    x = -x;
  }
  do {
    buffor[i++] = (x % 10) + '0';
    x /= 10;
  } while (x);
  i--;
  if (neg) putchar('-');
  while (i >= 0) putchar(buffor[i--]);
  putchar('\n');
}
int h, w, k, x, y;
char a[801][801];
int d[801][801];
void input() {
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
}
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
void bfs() {
  queue<pair<int, int> > q;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) d[i][j] = -1;
  d[x][y] = 0;
  q.push(pair<int, int>(x, y));
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    if (d[u.first][u.second] == k) return;
    for (int hi = 0; hi <= 3; hi++) {
      int nx = u.first + dx[hi], ny = u.second + dy[hi];
      if (nx < 1 or nx > h or ny < 1 or ny > w or d[nx][ny] != -1 or
          a[nx][ny] == '#')
        continue;
      d[nx][ny] = d[u.first][u.second] + 1;
      q.push(pair<int, int>(nx, ny));
    }
  }
}
void solve() {
  bfs();
  int res = 1e8;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      if (a[i][j] != '#' and d[i][j] <= k) {
        int dist;
        dist = min(i - 1, h - i);
        dist = min(dist, min(j - 1, w - j));
        int tmp = dist / k + ((dist % k == 0) ? 0 : 1);
        res = min(res, tmp);
      }
  cout << res + 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  input();
  solve();
  return 0;
}
