#include <bits/stdc++.h>
using namespace std;
const int N = 805;
const int mod = 1e9;
const int inf = 1e9;
int n, m, k, ans, dis[N][N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
queue<pair<int, int> > Q;
pair<int, int> st;
bool bo[N][N];
char s[N][N];
void bfs() {
  Q.push(st);
  bo[st.first][st.second] = 1;
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (xx < 1 || xx > n || yy < 1 || yy > m || s[xx][yy] == '#' ||
          bo[xx][yy])
        continue;
      bo[xx][yy] = 1;
      dis[xx][yy] = dis[x][y] + 1;
      Q.push(make_pair(xx, yy));
    }
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (s[i][j] == 'S') st = make_pair(i, j);
  ans = inf;
  bfs();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == '#') continue;
      if (dis[i][j] > k) continue;
      int tmp = min(i - 1, n - i);
      tmp = min(tmp, j - 1);
      tmp = min(tmp, m - j);
      ans = min(ans, 1 + (tmp + k - 1) / k);
    }
  }
  cout << ans;
}
