#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long Inf = 1e18;
const int N = 1e3 + 10;
const int mod = 0;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
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;
};
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; }
void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); }
void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); }
int n, m, k, sx, sy, dis[N][N], dn[4] = {1, 0, 0, -1}, dm[4] = {0, 1, -1, 0};
char s[N][N];
bool vis[N][N];
void bfs() {
  queue<pair<int, int> > q;
  q.push(make_pair(sx, sy));
  vis[sx][sy] = 1;
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    for (int d = 0; d < 4; d++) {
      int x = u.first + dn[d], y = u.second + dm[d];
      if (!vis[x][y] && s[x][y] != '#') {
        dis[x][y] = dis[u.first][u.second] + 1;
        vis[x][y] = 1;
        if (dis[x][y] != k) q.push(make_pair(x, y));
      }
    }
  }
}
int main() {
  cin >> n >> m >> k;
  memset(s, '#', sizeof(s));
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= m; j++)
      if (s[i][j] == 'S') sx = i, sy = j;
  }
  bfs();
  int ans = inf;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (vis[i][j]) {
        if (i == 1 || i == n || j == 1 || j == m) ans = min(ans, 1);
        ans =
            min(ans, min(i - 1, min(j - 1, min(n - i, m - j)) - 1) / k + 1 + 1);
      }
  printf("%d\n", ans);
  return 0;
}
