#include <bits/stdc++.h>
using namespace std;
struct data {
  int x, y;
  data() {}
  data(int x, int y) : x(x), y(y) {}
} q[100100];
int dist[810][810], k, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, w, h, l, r;
char s[810][810];
int get(int x) {
  if (x % k == 0)
    return x / k;
  else
    return x / k + 1;
}
int main() {
  scanf("%d%d%d", &w, &h, &k);
  for (int i = 1; i <= w; ++i) scanf("%s", s[i] + 1);
  for (int i = 1; i <= w; ++i)
    for (int j = 1; j <= h; ++j)
      if (s[i][j] == 'S') q[r++] = data(i, j), dist[i][j] = 1;
  while (l < r) {
    data d = q[l++];
    if (dist[d.x][d.y] == k + 1) continue;
    for (int i = 0; i < 4; ++i) {
      int nx = d.x + dx[i], ny = d.y + dy[i];
      if (nx < 1 || ny < 1 || nx > w || ny > h) continue;
      if (dist[nx][ny] || s[nx][ny] != '.') continue;
      dist[nx][ny] = dist[d.x][d.y] + 1;
      q[r++] = data(nx, ny);
    }
  }
  int ans = 1 << 30;
  for (int i = 1; i <= w; ++i)
    for (int j = 1; j <= h; ++j)
      if (dist[i][j])
        ans = min(ans, get(min(min(i - 1, j - 1), min(w - i, h - j))) +
                           dist[i][j] - 1);
  printf("%d", ans);
}
