#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
const int INF = 1 << 30;
inline char get(void) {
  return getchar();
  static char buf[100000], *S = buf, *T = buf;
  if (S == T) {
    T = (S = buf) + fread(buf, 1, 100000, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
template <typename T>
inline void read(T &x) {
  static char c;
  x = 0;
  int sgn = 0;
  for (c = get(); c < '0' || c > '9'; c = get())
    if (c == '-') sgn = 1;
  for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
  if (sgn) x = -x;
}
char ch[N][N];
int mp[N][N], vis[N][N];
int n, m, k, sx, sy, px, py, mn, dis;
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
inline int Min(int a, int b) { return a < b ? a : b; }
void dfs(int x, int y, int step) {
  if (vis[x][y]) return;
  vis[x][y] = 1;
  if (step > k || mp[x][y] || x < 1 || y < 1 || x > n || y > m) return;
  mn = Min(Min(Min(x - 1, y - 1), Min(n - x, m - y)), mn);
  for (int i = 0; i < 4; i++) dfs(x + d[i][0], y + d[i][1], step + 1);
}
int main(void) {
  read(n);
  read(m);
  read(k);
  for (int i = 1; i <= n; i++) scanf("%s", ch[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (ch[i][j] == 'S')
        sx = i, sy = j;
      else if (ch[i][j] == '#')
        mp[i][j] = 1;
  mn = INF;
  dfs(sx, sy, 0);
  cout << 1 + (mn + k - 1) / k << endl;
  return 0;
}
