#include <bits/stdc++.h>
using namespace std;
const int maxn = 810;
char s[maxn][maxn];
struct node {
  int x, y;
  int step = 0;
} st, en;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
bool vis[maxn][maxn];
int minn = 810;
int h, w, k;
void bfs() {
  queue<node> q;
  q.push(st);
  while (!q.empty()) {
    st = q.front();
    q.pop();
    vis[st.x][st.y] = true;
    minn = min(min(minn, st.x + 1),
               min(min(st.y + 1, h - st.x - 1), w - st.y - 1));
    for (int i = 0; i < 4; ++i) {
      int xx = dx[i] + st.x;
      int yy = dy[i] + st.y;
      if (xx < 0 || yy < 0 || xx >= h || yy >= w || vis[xx][yy] ||
          s[xx][yy] == '#') {
        continue;
      }
      en.x = xx;
      en.y = yy;
      en.step = st.step + 1;
      if (en.step <= k) q.push(en);
    }
  }
}
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 0; i < h; ++i) {
    scanf("%s", s[i]);
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s[i][j] == 'S') {
        st.x = i;
        en.y = j;
        break;
      }
    }
  }
  bfs();
  printf("%d", 1 + (minn + 1) / k);
  return 0;
}
