#include <bits/stdc++.h>
using namespace std;
inline void write(int a) {
  if (a >= 10) write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(int a) {
  write(a);
  puts("");
}
inline int read() {
  int x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
int ans = 10000000, n, m, k, S1[4] = {0, 0, 1, -1}, S2[4] = {1, -1, 0, 0}, s1,
    s2;
bool vis[1000][1000];
char ch[1000][1000];
inline void dfs(int x, int y) {
  if (vis[x][y] || (ch[x][y] != '.' && ch[x][y] != 'S')) return;
  vis[x][y] = true;
  if (x == 1 || x == n || y == 1 || y == m) {
    ans = min(ans, 1);
  }
  ans = min(ans, 1 + min((x - 2) / k + 1,
                         min((y - 2) / k + 1,
                             min((n - x - 1) / k + 1, (n - y - 1) / k + 1))));
  for (int i = 0; i < 4; i++) dfs(x + S1[i], y + S2[i]);
}
int main() {
  n = read();
  m = read();
  k = read();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) scanf("\n%c", &ch[i][j]);
    for (int j = 1; j <= m; j++)
      if (ch[i][j] == 'S') {
        s1 = i;
        s2 = j;
        if (s1 == 1 || s1 == n || s2 == 1 || s2 == m) {
          puts("0");
          return 0;
        }
      }
  }
  dfs(s1, s2);
  writeln(ans);
}
