#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chkmax(T& a, T b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
bool chkmin(T& a, T b) {
  return a > b ? a = b, 1 : 0;
}
const int N = 800;
const int oo = 0x3f3f3f3f;
template <typename T>
T read() {
  T n(0), f(1);
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) n = n * 10 + ch - 48;
  return n * f;
}
int sx, sy;
int n, m, k, ans = INT_MAX;
bool vis[N + 5][N + 5];
char grid[N + 5][N + 5];
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
void dfs(int x, int y, int d) {
  vis[x][y] = true;
  if (d > k) return;
  chkmin(ans, x);
  chkmin(ans, y);
  chkmin(ans, n - 1 - x);
  chkmin(ans, m - 1 - y);
  for (int i = 0; i < 4; ++i) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx < 0 || nx >= n || ny < 0 || ny >= m || grid[nx][ny] == '#' ||
        vis[nx][ny])
      continue;
    dfs(nx, ny, d + 1);
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%s", grid[i]);
    for (int j = 0; j < m; ++j) {
      if (grid[i][j] == 'S') {
        sx = i, sy = j;
        break;
      }
    }
  }
  dfs(sx, sy, 0);
  printf("%d\n", int(ceil(1. * ans / k)) + 1);
  return 0;
}
