#include <bits/stdc++.h>
using namespace std;
int h, w, k;
bool t[810][810], go[810][810];
void DFS(int y, int x, int len) {
  go[y][x] = true;
  if (len == k) return;
  int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
  for (int i = 0; i < 4; i++) {
    int f = y + dy[i], g = x + dx[i];
    if (f >= 0 && f < h && g >= 0 && g < w) {
      if (t[f][g]) DFS(f, g, len + 1);
    }
  }
}
int main() {
  cin >> h >> w >> k;
  int f, g;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char z;
      cin >> z;
      if (z == '#')
        t[i][j] = 0;
      else
        t[i][j] = 1;
      if (z == 'S') f = i, g = j;
      go[i][j] = false;
    }
  }
  if (f == 0 || g == 0 || f == h - 1 || g == w - 1) {
    cout << "0" << endl;
    return 0;
  }
  DFS(f, g, 0);
  int ans = 114514;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (go[i][j]) {
        int cnt = min(i, h - i - 1);
        cnt = min(cnt, j);
        cnt = min(cnt, w - j - 1);
        if (cnt % k == 0)
          cnt = cnt / k + 1;
        else
          cnt = cnt / k + 2;
        ans = min(ans, cnt);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
