#include <bits/stdc++.h>
using namespace std;
int H, W, K;
char maze[900][900];
int si, sj;
int ans = 1000000;
void left(int xi, int xj, int step) {
  int ni, nj;
  for (int i = 0; i < K; i++) {
    ni = xi - i;
    if (ni < 0) {
      ans = min(ans, step);
      return;
    }
    if (maze[ni][xj]) {
      ni--;
      break;
    }
  }
  xi = ni;
  for (int i = 0; i < K; i++) {
    ni--;
    if (ni < 0) break;
    if (maze[ni][xj])
      maze[ni][xj] = 0;
    else {
      i++;
      continue;
    }
  }
  left(xi, xj, step + 1);
}
void right(int xi, int xj, int step) {
  int ni, nj;
  for (int i = 0; i < K; i++) {
    ni = xi + i;
    if (ni >= W) {
      ans = min(ans, step);
      return;
    }
    if (maze[ni][xj]) {
      ni++;
      break;
    }
  }
  xi = ni;
  for (int i = 0; i < K; i++) {
    ni++;
    if (ni >= W) break;
    if (maze[ni][xj])
      maze[ni][xj] = 0;
    else {
      i++;
      continue;
    }
  }
  right(xi, xj, step + 1);
}
void up(int xi, int xj, int step) {
  int ni, nj;
  for (int i = 0; i < K; i++) {
    nj = xj - i;
    if (nj < 0) {
      ans = min(ans, step);
      return;
    }
    if (maze[xi][nj]) {
      nj--;
      break;
    }
  }
  xj = nj;
  for (int i = 0; i < K; i++) {
    nj--;
    if (nj < 0) break;
    if (maze[xi][nj])
      maze[xi][nj] = 0;
    else {
      i++;
      continue;
    }
  }
  up(xi, xj, step + 1);
}
void down(int xi, int xj, int step) {
  int ni, nj;
  for (int i = 0; i < K; i++) {
    nj = xj + i;
    if (nj >= H) {
      ans = min(ans, step);
      return;
    }
    if (maze[xi][nj]) {
      nj++;
      break;
    }
  }
  xj = nj;
  for (int i = 0; i < K; i++) {
    nj++;
    if (nj >= H) break;
    if (maze[xi][nj])
      maze[xi][nj] = 0;
    else {
      i++;
      continue;
    }
  }
  down(xi, xj, step + 1);
}
int main() {
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) {
      cin >> maze[i][j];
      if (maze[i][j] == 'S') si = i, sj = j;
    }
  left(si, sj, 0);
  right(si, sj, 0);
  up(si, sj, 0);
  down(si, sj, 0);
  cout << ans << endl;
  return 0;
}
