#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7ffffff;
int ans = INF;
int movee[5][3];
bool visited[848][848];
int r, c, k;
int sx, sy;
int a[848][848];
string s;
queue<int> q;
void bfs(int first, int second, int step) {
  q.push(first);
  q.push(second);
  q.push(0);
  int cur_ans, cx, cy, cd, xx, yy, dd;
  while (!q.empty()) {
    cx = q.front();
    q.pop();
    cy = q.front();
    q.pop();
    cd = q.front();
    q.pop();
    visited[cx][cy] = true;
    cur_ans = cx - 1;
    cur_ans = min(cur_ans, cy - 1);
    cur_ans = min(cur_ans, r - cx);
    cur_ans = min(cur_ans, c - cy);
    ans = min(ans, cur_ans);
    if (cd == k) continue;
    for (int i = 1; i <= 4; i++) {
      xx = cx + movee[i][1];
      yy = cy + movee[i][2];
      if (xx < 1 || xx > r || yy < 1 || yy > c) continue;
      if (a[xx][yy] == 1 || visited[xx][yy]) continue;
      q.push(xx);
      q.push(yy);
      q.push(dd + 1);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  int i, j;
  movee[1][1] = 1;
  movee[1][2] = 0;
  movee[2][1] = 0;
  movee[2][2] = 1;
  movee[3][1] = -1;
  movee[3][2] = 0;
  movee[4][1] = 0;
  movee[4][2] = -1;
  cin >> r >> c >> k;
  for (i = 1; i <= r; i++) {
    cin >> s;
    for (j = 1; j <= c; j++) {
      if (s[j - 1] == '#') a[i][j] = 1;
      if (s[j - 1] == '.') a[i][j] = 2;
      if (s[j - 1] == 'S') {
        sx = i;
        sy = j;
        a[i][j] = 2;
      }
    }
  }
  bfs(sx, sy, 0);
  int fans = 1 + (ans + k - 1) / k;
  cout << fans << endl;
  return 0;
}
