#include <bits/stdc++.h>
using namespace std;
const int N = 805;
int n, m, k;
char a[N][N];
int sx, sy;
int d[N][N];
const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
void from_border() {
  queue<pair<int, int> > q;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) d[i][j] = -1;
  for (int j = 2; j <= m - 1; ++j) d[1][j] = 1, q.push(pair<int, int>(1, j));
  for (int j = 2; j <= m - 1; ++j) d[n][j] = 1, q.push(pair<int, int>(n, j));
  for (int i = 1; i <= n; ++i) d[i][1] = 1, q.push(pair<int, int>(i, 1));
  for (int i = 1; i <= n; ++i) d[i][m] = 1, q.push(pair<int, int>(i, m));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int dir = 0; dir < 4; ++dir) {
      int nx = x + dx[dir], ny = y + dy[dir];
      if (nx < 1 || nx > n || ny < 1 || ny > m || d[nx][ny] != -1) continue;
      d[nx][ny] = d[x][y] + 1;
      q.push(pair<int, int>(nx, ny));
    }
  }
}
int frS[N][N];
void from_S() {
  queue<pair<int, int> > q;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) frS[i][j] = -1;
  frS[sx][sy] = 0;
  q.push(pair<int, int>(sx, sy));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int dir = 0; dir < 4; ++dir) {
      int nx = x + dx[dir], ny = y + dy[dir];
      if (nx < 1 || nx > n || ny < 1 || ny > m || frS[nx][ny] != -1 ||
          a[nx][ny] == '#')
        continue;
      frS[nx][ny] = frS[x][y] + 1;
      q.push(pair<int, int>(nx, ny));
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j];
      if (a[i][j] == 'S') sx = i, sy = j;
    }
  from_border();
  from_S();
  int ans = 1e9;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (frS[i][j] != -1 && frS[i][j] <= k) {
        int cur = frS[i][j] / k + (frS[i][j] % k != 0);
        for (int dir = 0; dir < 4; ++dir) {
          int nx = i + dx[dir], ny = j + dy[dir];
          if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
          int add = d[nx][ny] / k + (d[nx][ny] % k != 0) + (a[nx][ny] == '#');
          ans = min(ans, add + cur);
        }
      }
  cout << ans << endl;
}
