#include <bits/stdc++.h>
using namespace std;
char g[805][805];
int h, w, k, ans = 1000000007;
int vis[805][805];
queue<pair<int, int> > q;
void bfs(pair<int, int> z) {
  q.push(z);
  vis[z.first][z.second] = 1;
  while (!q.empty()) {
    pair<int, int> e = q.front();
    q.pop();
    int u = e.first, v = e.second;
    if (vis[u][v] + 1 > k || g[u][v] == '#') continue;
    int d = min(min(u, h - 1 - u), min(v, w - 1 - v));
    if (d == 0)
      ans = 1;
    else
      ans = min(ans, d / k + (d % k != 0) + 1);
    if (u > 0) {
      vis[u - 1][v] = vis[u][v] + 1;
      q.push(pair<int, int>(u - 1, v));
    }
    if (u + 1 < h) {
      vis[u + 1][v] = vis[u][v] + 1;
      q.push(pair<int, int>(u + 1, v));
    }
    if (v > 0) {
      vis[u][v - 1] = vis[u][v] + 1;
      q.push(pair<int, int>(u, v - 1));
    }
    if (v + 1 < w) {
      vis[u][v + 1] = vis[u][v] + 1;
      q.push(pair<int, int>(u, v + 1));
    }
  }
}
int main() {
  int i, j;
  int u, v;
  scanf("%d%d%d", &h, &w, &k);
  for (i = 0; i < h; i++) {
    scanf("%s", g[i]);
    for (j = 0; j < w; j++) {
      if (g[i][j] == 'S') {
        u = i;
        v = j;
      }
    }
  }
  bfs(pair<int, int>(u, v));
  printf("%d\n", ans);
  return 0;
}
