#include <bits/stdc++.h>
const int MAX = 800 + 10;
using namespace std;
int H, W, K;
int moves[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
pair<int, int> que[MAX * MAX];
int dist[MAX][MAX];
bool vis[MAX][MAX];
char mat[MAX][MAX];
int bfs(int sx, int sy) {
  int b = 0, e = 0;
  vis[sx][sy] = true;
  memset(dist, -1, sizeof(dist));
  dist[sx][sy] = 0;
  que[b++] = {sx, sy};
  while (e < b) {
    auto cur = que[e++];
    if (cur.first == 1 or cur.second == W or cur.second == 1 or
        cur.first == H) {
      return dist[cur.first][cur.second];
    }
    for (int i = 0; i < 4; i++) {
      pair<int, int> ncur;
      ncur.first = cur.first + moves[i][0],
      ncur.second = cur.second + moves[i][1];
      if (ncur.first < 1 or ncur.first > H or ncur.second < 1 or
          ncur.second > W)
        continue;
      if (vis[ncur.first][ncur.second]) continue;
      if (dist[cur.first][cur.second] < K and
          mat[ncur.first][ncur.second] == '#')
        continue;
      que[b++] = ncur;
      vis[ncur.first][ncur.second] = true;
      dist[ncur.first][ncur.second] = dist[cur.first][cur.second] + 1;
    }
  }
  for (int i = 0; i < b; i++) dist[que[i].first][que[i].second] = K;
  b = 1;
  while (e < b) {
    auto cur = que[e++];
    if (cur.first == 1 or cur.second == W or cur.second == 1 or
        cur.first == H) {
      return dist[cur.first][cur.second];
    }
    for (int i = 0; i < 4; i++) {
      pair<int, int> ncur;
      ncur.first = cur.first + moves[i][0],
      ncur.second = cur.second + moves[i][1];
      if (ncur.first < 1 or ncur.first > H or ncur.second < 1 or
          ncur.second > W)
        continue;
      if (vis[ncur.first][ncur.second]) continue;
      que[b++] = ncur;
      vis[ncur.first][ncur.second] = true;
      dist[ncur.first][ncur.second] = dist[cur.first][cur.second] + 1;
    }
  }
  assert(1 == 0);
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> H >> W >> K;
  int sx, sy;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++) {
      cin >> mat[i][j];
      if (mat[i][j] == 'S') {
        mat[i][j] = '.';
        sx = i, sy = j;
      }
    }
  int ans = bfs(sx, sy);
  cout << (ans + K - 1) / K << '\n';
}
