#include <bits/stdc++.h>
using namespace std;
const long long N = 8e2 + 10;
long long n, m, dis[N][N], k;
long long dx[4] = {0, 0, -1, 1};
long long dy[4] = {-1, 1, 0, 0};
char mat[N][N];
bool mark[N][N];
bool isval(long long i, long long j) {
  return !mark[i][j] && i < n && j < m && i >= 0 && j >= 0 && mat[i][j] == '.';
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> k;
  pair<long long, long long> st;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) {
      cin >> mat[i][j];
      if (mat[i][j] == 'S') st = {i, j};
    }
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) dis[i][j] = 1e18;
  queue<pair<long long, long long>> q;
  q.push(st);
  dis[st.first][st.second] = 0;
  mark[st.first][st.second] = true;
  while ((long long)(q.size())) {
    pair<long long, long long> p = q.front();
    for (long long i = 0; i < 4; i++)
      if (isval(p.first + dx[i], p.second + dy[i])) {
        dis[p.first + dx[i]][p.second + dy[i]] = dis[p.first][p.second] + 1;
        mark[p.first + dx[i]][p.second + dy[i]] = true;
        q.push({p.first + dx[i], p.second + dy[i]});
      }
    q.pop();
  }
  long long ans = 1e18;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) {
      if (dis[i][j] <= k) {
        long long x = min({i, j, n - i + 1, m - j + 1});
        ans = min(ans, (x + k - 1) / k + 1);
      }
    }
  cout << ans;
}
