#include <bits/stdc++.h>
using namespace std;
pair<pair<int, int>, int> q[810 * 810];
bool vis[810][810];
const int tx[4] = {1, 0, -1, 0}, ty[4] = {0, 1, 0, -1};
int l, r, n, m, k, ans = 1e9;
char mp[810][810];
int main() {
  int i, j, x, y, d;
  scanf("%d%d%d", &n, &m, &k);
  for (i = 1; i <= n; i++) {
    scanf("%s", mp[i] + 1);
    for (j = 1; j <= m; j++)
      if (mp[i][j] == 'S') q[1] = make_pair(make_pair(i, j), 0);
  }
  x = q[1].first.first;
  y = q[1].first.second;
  ans = min(ans, 1 + (n - 1 - x) / k + 1);
  ans = min(ans, 1 + (x - 2) / k + 1);
  ans = min(ans, 1 + (m - 1 - y) / k + 1);
  ans = min(ans, 1 + (y - 2) / k + 1);
  l = 1;
  r = 1;
  while (l <= r) {
    if (q[l].second == k) break;
    for (i = 0; i < 4; i++) {
      x = q[l].first.first + tx[i];
      y = q[l].first.second + ty[i];
      d = q[l].second + 1;
      if (x < 1 || x > n || y < 1 || y > m || vis[x][y] || mp[x][y] == '#')
        continue;
      if (x == 1 || x == n || y == 0 || y == m) {
        printf("%d\n", d);
        return 0;
      }
      vis[x][y] = 1;
      q[++r] = make_pair(make_pair(x, y), d);
      ans = min(ans, 1 + (n - 1 - x) / k + 1);
      ans = min(ans, 1 + (x - 2) / k + 1);
      ans = min(ans, 1 + (m - 1 - y) / k + 1);
      ans = min(ans, 1 + (y - 2) / k + 1);
    }
    l++;
  }
  printf("%d\n", ans);
  return 0;
}
