#include <bits/stdc++.h>
using namespace std;
long long h, w, k;
string grid[810];
bool vis[810][810];
long long movey[4] = {0, 0, -1, 1};
long long movex[4] = {-1, 1, 0, 0};
long long magic(long long y, long long x) {
  queue<vector<long long> > wow;
  wow.push({y, x, 0});
  while (!wow.empty()) {
    long long nowy = wow.front()[0];
    long long nowx = wow.front()[1];
    long long nowsteps = wow.front()[2];
    wow.pop();
    if (vis[nowy][nowx] || grid[nowy][nowx] == '#') continue;
    vis[nowy][nowx] = 1;
    if (nowy == 0 || nowx == 0 || nowy == h - 1 || nowx == w - 1)
      return nowsteps;
    for (long long i = 0; i < 4; i++) {
      long long targety = nowy + movey[i];
      long long targetx = nowx + movex[i];
      if (targety < 0 || targetx < 0 || targety > h - 1 || targetx > w - 1)
        continue;
      if (vis[targety][targetx] || grid[targety][targetx] == '#') continue;
      wow.push({targety, targetx, nowsteps + 1});
    }
  }
  return INT_MAX;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long starty2 = -1;
  long long startx2 = -1;
  long long starty = -1;
  long long startx = -1;
  cin >> h >> w >> k;
  for (long long i = 0; i < h; i++) {
    cin >> grid[i];
    if (starty >= 0) continue;
    for (long long j = 0; j < w; j++) {
      if (grid[i][j] == 'S') {
        starty = i;
        startx = j;
        starty2 = i;
        startx2 = j;
        break;
      }
    }
  }
  long long steps;
  long long mindist = INT_MAX;
  starty = starty2;
  startx = startx2;
  steps = k;
  mindist = min(mindist, starty);
  while (steps > 0) {
    if (starty > 0 && grid[starty - 1][startx] != '#') {
      starty--;
      steps--;
      mindist = min(mindist, starty);
    } else
      break;
  }
  starty = starty2;
  startx = startx2;
  steps = k;
  mindist = min(mindist, h - starty - 1);
  while (steps > 0) {
    if (starty < h - 1 && grid[starty + 1][startx] != '#') {
      starty++;
      steps--;
      mindist = min(mindist, h - starty - 1);
    } else
      break;
  }
  starty = starty2;
  startx = startx2;
  steps = k;
  mindist = min(mindist, startx);
  while (steps > 0) {
    if (startx > 0 && grid[starty][startx - 1] != '#') {
      startx--;
      steps--;
      mindist = min(mindist, startx);
    } else
      break;
  }
  starty = starty2;
  startx = startx2;
  steps = k;
  mindist = min(mindist, w - startx - 1);
  while (steps > 0) {
    if (startx < w - 1 && grid[starty][startx + 1] != '#') {
      startx++;
      steps--;
      mindist = min(mindist, w - startx - 1);
    } else
      break;
  }
  starty = starty2;
  startx = startx2;
  long long temp = magic(starty, startx);
  long long ans =
      min(1 + mindist / k + (mindist % k > 0), temp / k + (temp % k > 0));
  cout << ans << endl;
}
