#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int maxn = 1e5 + 10;
int n, m, k, sx, sy;
char mp[805][805];
int vis[805][805], f[805][805];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int mask;
struct node {
  int x, y, step;
};
void bfs2() {
  queue<node> q;
  q.push(node{sx, sy, 0});
  vis[sx][sy] = 1;
  f[sx][sy] = 1;
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y, s = q.front().step;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tx = x + dx[i], ty = y + dy[i];
      if (tx < 1 || tx > n || ty < 1 || ty > m || mp[tx][ty] == '#' ||
          vis[tx][ty])
        continue;
      if (tx == 1 || tx == n || ty == 1 || ty == m) {
        mask = 1;
        return;
      }
      vis[tx][ty] = 1;
      s++;
      if (s <= k) f[tx][ty] = 1;
      q.push(node{tx, ty, s});
    }
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf(" %c", &mp[i][j]);
      if (mp[i][j] == 'S') sx = i, sy = j;
    }
  memset(vis, 0, sizeof(vis));
  bfs2();
  if (mask) {
    cout << 1 << endl;
    return 0;
  }
  int t;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (f[i][j]) {
        t = min(t, min(n - i, min(i - 1, min(m - j, j - 1))));
      }
    }
  int ans;
  if (t % k == 0)
    ans = t / k;
  else
    ans = (t + k) / k;
  cout << 1 + ans << endl;
  return 0;
}
