#include <bits/stdc++.h>
using namespace std;
long long int i, j, k, z, t, n, m, sum, ans, x, y, maxm = 0, minm = 1e15;
bool flag;
long long int dist[805][805];
vector<pair<long long int, long long int>> reach;
char a[805][805];
inline long long int ceil(long long int x, long long int y) {
  if (x % y)
    return x / y + 1;
  else
    return x / y;
}
bool check(long long int x, long long int y) {
  if (x < 1 || x > n || y < 1 || y > m)
    return false;
  else if (a[x][y] == '#')
    return false;
  else
    return true;
}
long long int dx[4] = {1, 0, -1, 0};
long long int dy[4] = {0, 1, 0, -1};
void bfs(long long int x, long long int y) {
  dist[x][y] = 0;
  priority_queue<
      pair<long long int, pair<long long int, long long int>>,
      vector<pair<long long int, pair<long long int, long long int>>>,
      greater<pair<long long int, pair<long long int, long long int>>>>
      Q;
  Q.push({0, {x, y}});
  while (!Q.empty()) {
    auto c = Q.top();
    Q.pop();
    z = c.first;
    x = c.second.first;
    y = c.second.second;
    if (z > k)
      continue;
    else
      reach.push_back({x, y});
    for (long long int i = 0; i < 4; i++)
      if (check(x + dx[i], y + dy[i]))
        if (dist[x + dx[i]][y + dy[i]] > z + 1)
          dist[x + dx[i]][y + dy[i]] = z + 1,
                              Q.push({z + 1, {x + dx[i], y + dy[i]}});
  }
}
int main() {
  scanf("%lld%lld%lld", &n, &m, &k);
  for (long long int i = 1; i < n + 1; i++)
    for (long long int j = 1; j < m + 1; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') x = i, y = j;
      dist[i][j] = 1e15;
    }
  bfs(x, y);
  ans = 1e15;
  for (auto c : reach) {
    ans = min(ans, min(ceil(c.first - 1, k), ceil(c.second - 1, k)) + 1);
  }
  printf("%lld\n", long long int(ans));
}
