#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
int n, m, stx, sty, step[805][805], mx, res = 2e9;
char grid[805][805];
queue<int> q;
inline bool out(int x, int y) { return x < 0 || x == n || y < 0 || y == m; }
void Init() {
  cin >> n >> m >> mx;
  memset(step, -1, sizeof(step));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> grid[i][j];
      if (grid[i][j] == 'S') stx = i, sty = j;
    }
  q.push(stx), q.push(sty);
  step[stx][sty] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int y = q.front();
    q.pop();
    if (step[x][y] == mx + 1) break;
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i], yy = y + dy[i];
      if (!out(xx, yy) && step[xx][yy] == -1 && grid[xx][yy] != '#') {
        step[xx][yy] = step[x][y] + 1;
        q.push(xx);
        q.push(yy);
      }
    }
  }
  while (!q.empty()) q.pop();
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (step[i][j] <= mx && step[i][j] != -1) q.push(i), q.push(j);
}
int main() {
  Init();
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int y = q.front();
    q.pop();
    res = min(res, (min(min(abs(x - n), x), min(y, abs(y - m))) + mx - 1) / mx);
  }
  printf("%d", res + 1);
  return 0;
}
