#include <bits/stdc++.h>
using namespace std;
long long h, w, k;
string grid[810];
bool vis[810][810];
bool magic(long long y, long long x, long long steps) {
  if (grid[y][x] == '#') return 0;
  if (y == 0 || x == 0 || y == h - 1 || x == w - 1) return 1;
  if (vis[y][x]) return 0;
  vis[y][x] = 1;
  if (steps == 0) return 0;
  return magic(y - 1, x, steps - 1) || magic(y + 1, x, steps - 1) ||
         magic(y, x - 1, steps - 1) || magic(y, x + 1, steps - 1);
}
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;
  }
  long long ans = 1;
  starty = starty2;
  startx = startx2;
  memset(vis, 0, sizeof(vis));
  if (!magic(starty, startx, k)) ans += mindist / k + (mindist % k > 0);
  cout << ans << endl;
}
