#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
char ch[1111][1111];
int used[1111][1111];
int step[1111][1111];
queue<pair<int, int> > q;
int main() {
  memset(used, 0, sizeof(used));
  memset(step, 0, sizeof(step));
  int n, m, k, ans = 1000000, sx, sy;
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> ch[i][j];
      if (ch[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  q.push(make_pair(sx, sy));
  used[sx][sy] = 1;
  while (!q.empty()) {
    pair<int, int> hh = q.front();
    q.pop();
    int x = hh.first;
    int y = hh.second;
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (!xx || !yy || xx > n || yy > m || ch[xx][yy] == '#') continue;
      if (!used[xx][yy]) {
        used[xx][yy] = 1;
        step[xx][yy] = step[x][y] + 1;
        q.push(make_pair(xx, yy));
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (step[i][j] || ch[i][j] == 'S') {
        int dis = min(abs(i - 1), min(abs(n - i), min(abs(j - 1), abs(m - j))));
        int step = (dis + k - 1) / k;
        ans = min(ans, step + 1);
      }
    }
  }
  cout << ans;
  return 0;
}
