#include <bits/stdc++.h>
using namespace std;
int di[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int dj[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int dp[800][800], dp2[800][800];
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  char mat[h][w];
  int si, sj;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> mat[i][j];
      if (mat[i][j] == 'S') si = i, sj = j;
    }
  }
  memset(dp, 0x3f, sizeof(dp));
  memset(dp2, 0x3f, sizeof(dp2));
  dp[si][sj] = dp2[si][sj] = 0;
  queue<pair<int, int> > q, tmp;
  q.push({si, sj});
  tmp.push({si, sj});
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int i = p.first, j = p.second;
    for (int k = 0; k < 4; k++) {
      int ni = i + di[k], nj = j + dj[k];
      if (!(0 <= ni && ni < h) || !(0 <= nj && nj < w) || mat[ni][nj] == '#' ||
          dp[ni][nj] <= dp[i][j] + 1)
        continue;
      dp[ni][nj] = dp[i][j] + 1;
      if (dp[ni][nj] < k) q.push({ni, nj});
      tmp.push({ni, nj});
      dp2[ni][nj] = 0;
    }
  }
  q = tmp;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int i = p.first, j = p.second;
    if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
      cout << (dp2[i][j] + k - 1) / k << endl;
      return 0;
    }
    for (int k = 0; k < 4; k++) {
      int ni = i + di[k], nj = j + dj[k];
      if ((0 <= ni && ni < h) && (0 <= nj && nj < w) &&
          dp2[ni][nj] > dp2[i][j] + 1) {
        dp2[ni][nj] = dp2[i][j] + 1;
        q.push({ni, nj});
      }
    }
  }
}
