#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}, u[850][850];
char a[850][850];
struct node {
  int x;
  int y;
  int step;
};
int judge(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m) return 1;
  if (u[x][y] == 1 || a[x][y] == '#') return 1;
  return 0;
}
void bfs() {
  node init;
  queue<node> q;
  node next;
  init.x = x;
  init.y = y;
  init.step = 0;
  u[x][y] = 1;
  q.push(init);
  while (!q.empty()) {
    node tmp = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      next.x = tmp.x + dir[i][0];
      next.y = tmp.y + dir[i][1];
      if (judge(next.x, next.y)) continue;
      a[next.x][next.y] = 'c';
      next.step = tmp.step + 1;
      u[next.x][next.y] = 1;
      if (next.step < k) q.push(next);
    }
  }
}
int main() {
  while (cin >> n >> m >> k) {
    memset(u, 0, sizeof(u));
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> a[i][j];
        if (a[i][j] == 'S') x = i, y = j;
      }
    }
    bfs();
    int ans = 1000;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i][j] == 'S' || a[i][j] == 'c') {
          for (int l = 0; l < 4; l++) {
            int num = 0;
            if (a[i + dir[l][0]][j + dir[l][1]] == '#' || a[i][j] == 'c')
              num = 1;
            if (a[i + dir[l][0]][j + dir[l][1]] == '.' && a[i][j] == 'S')
              continue;
            int numx = 0, numy = 0;
            if (dir[l][0] == -1)
              numx = i - 1;
            else if (dir[l][0] == 1)
              numx = n - i;
            if (dir[l][1] == -1)
              numy = j - 1;
            else if (dir[l][1] == 1)
              numy = m - j;
            num += (numx + numy) / k + (numx + numy) % k;
            ans = min(ans, num);
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
