#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> g(h);
  int sx, sy;
  for (int i = 0; i < h; i++) {
    cin >> g[i];
    for (int j = 0; j < w; j++) {
      if (g[i][j] == 'S') sx = i, sy = j;
    }
  }
  vector<vector<int>> dist(h, vector<int>(w, -1));
  dist[sx][sy] = 0;
  queue<pair<int, int>> que;
  que.emplace(sx, sy);
  int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
  while (que.size()) {
    auto cur = que.front();
    que.pop();
    if (dist[cur.first][cur.second] == k) continue;
    for (int i = 0; i < 4; i++) {
      int x = cur.first + dx[i];
      int y = cur.second + dy[i];
      if (x < 0 || x >= h || y < 0 || y >= w) continue;
      if (g[x][y] == '#') continue;
      if (dist[x][y] != -1) continue;
      dist[x][y] = dist[cur.first][cur.second] + 1;
      que.emplace(x, y);
    }
  }
  for (int i = 0; i < h; i++) {
    if (dist[i][0] == -1 && dist[i][w - 1] == -1) continue;
    cout << 1 << '\n';
    return 0;
  }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (dist[i][j] >= 0) {
        que.emplace(i, j);
        dist[i][j] = 0;
      }
    }
  while (que.size()) {
    auto cur = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int x = cur.first + dx[i];
      int y = cur.second + dy[i];
      if (x < 0 || x >= h || y < 0 || y >= w) continue;
      if (dist[x][y] != -1) continue;
      dist[x][y] = dist[cur.first][cur.second] + 1;
      que.emplace(x, y);
      if (x == 0 || x == h - 1 || y == 0 || y == w - 1) {
        cout << 1 + (dist[x][y] + k - 1) / k << '\n';
        return 0;
      }
    }
  }
  return 0;
}
