#include <bits/stdc++.h>
using namespace std;
const int N = 805;
int h, w, k;
char grid[N][N];
int v[N][N];
int foo(int x, int y) {
  int res = 0x3f3f3f3f;
  res = min(res, x);
  res = min(res, h - 1 - x);
  res = min(res, y);
  res = min(res, w - 1 - y);
  return (res + k - 1) / k;
}
int ans = 0x3f3f3f3f;
struct node {
  int x, y;
};
int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void bfs(int x, int y) {
  queue<node> q;
  q.push({x, y});
  memset(v, 0x3f3f3f3f, sizeof(v));
  v[x][y] = 0;
  ans = min(ans, foo(x, y));
  while (!q.empty()) {
    node cur = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int xx = cur.x + d[i][0];
      int yy = cur.y + d[i][1];
      if (xx >= 0 && xx < h && yy >= 0 && yy < w && v[xx][yy] != 0x3f3f3f3f &&
          grid[xx][yy] != '#') {
        ans = min(ans, foo(xx, yy));
        v[xx][yy] = v[cur.x][cur.y] + 1;
        if (v[xx][yy] < k) q.push({xx, yy});
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 0; i < h; i++) {
    scanf("%s", grid[i]);
  }
  int sx, sy;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  bfs(sx, sy);
  printf("%d\n", ans + 1);
}
