#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
constexpr int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
int dp[801][801][801];
bool used[801][801];
int main() {
  int h, w, K;
  cin >> h >> w >> K;
  std::vector<std::string> s(h);
  for (int i = 0; i < h; i++) cin >> s[i];
  int sx, sy;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] != 'S') continue;
      sx = i;
      sy = j;
      i = j = 1e9;
    }
  }
  used[sx][sy] = true;
  std::priority_queue<
      std::pair<int, std::pair<int, std::pair<int, int>>>,
      std::vector<std::pair<int, std::pair<int, std::pair<int, int>>>>,
      std::greater<std::pair<int, std::pair<int, std::pair<int, int>>>>>
      qu;
  qu.push({0, {0, {sx, sy}}});
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      for (int k = 0; k < 801; k++) dp[i][j][k] = 1e9;
  dp[sx][sy][0] = 0;
  while (!qu.empty()) {
    auto p = qu.top();
    qu.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.second.second.first + vy[i],
          ny = p.second.second.second + vx[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
      int block = (s[nx][ny] == '#');
      if (p.first + 1 >= dp[nx][ny][p.second.first + block]) continue;
      dp[nx][ny][p.second.first + block] = p.first + 1;
      qu.push({p.first + 1, {p.second.first + block, {nx, ny}}});
    }
  }
  int mi = 2e9;
  for (int i = 0; i < w; i++) {
    for (int j = 1; j < 801; j++) {
      mi = std::min(
          mi, std::max(std::max(2, (dp[0][i][j] + K - 1) / K), j / K + 1));
      mi = std::min(
          mi, std::max(std::max(2, (dp[i][0][j] + K - 1) / K), j / K + 1));
      mi = std::min(
          mi, std::max(std::max(2, (dp[h - 1][i][j] + K - 1) / K), j / K + 1));
      mi = std::min(
          mi, std::max(std::max(2, (dp[i][w - 1][j] + K - 1) / K), j / K + 1));
    }
    mi = std::min(mi, (dp[0][i][0] + K - 1) / K);
    mi = std::min(mi, (dp[i][0][0] + K - 1) / K);
    mi = std::min(mi, (dp[h - 1][i][0] + K - 1) / K);
    mi = std::min(mi, (dp[i][w - 1][0] + K - 1) / K);
  }
  cout << mi << endl;
  return 0;
}
