#include <bits/stdc++.h>
using namespace std;
char c[805][805];
int vis[805][805];
int xx[] = {0, 1, 0, -1};
int yy[] = {1, 0, -1, 0};
struct fa1 {
  int i, j;
} fa[805][805];
int ans = 1000000000;
int now = 0;
int h, w, k;
void dfs(int i, int j, int deep) {
  if (i == 0 || j == 0 || i == h - 1 || j == w - 1) {
    ans = min(ans, (now - 1 + k - 1) / k + (deep + k - 1) / k);
  }
  if (vis[i][j] >= 0 && deep >= vis[i][j]) return;
  vis[i][j] = deep;
  for (int i1 = 0; i1 < 4; i1++) {
    if (i + xx[i1] < h && i + xx[i1] >= 0 && j + yy[i1] >= 0 &&
        j + yy[i1] < w && c[i + xx[i1]][j + yy[i1]] != '#') {
      now++;
      dfs(i + xx[i1], j + yy[i1], deep);
      now--;
    } else if (deep + 1 <= k && i + xx[i1] < h && i + xx[i1] >= 0 &&
               j + yy[i1] >= 0 && j + yy[i1] < w &&
               c[i + xx[i1]][j + yy[i1]] == '#') {
      now++;
      dfs(i + xx[i1], j + yy[i1], deep + 1);
      now--;
    }
  }
}
int main() {
  int n;
  cin >> h >> w >> k;
  for (int i = 0; i < 801; i++)
    for (int j = 0; j < 801; j++) vis[i][j] = -1;
  for (int i = 0; i < h; i++) {
    cin >> c[i];
  }
  int bx, by;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      if (c[i][j] == 'S') {
        bx = i;
        by = j;
      }
  }
  dfs(bx, by, 0);
  cout << ans << endl;
  return 0;
}
