#include <bits/stdc++.h>
using namespace std;
const int N = 805;
int h, w, k;
char grid[N][N];
bool vis[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, dis;
};
void bfs(int x, int y) {
  queue<node> q;
  q.push({x, y, 0});
  vis[x][y] = 1;
  ans = min(ans, foo(x, y) + 1);
  while (!q.empty()) {
    node cur = q.front();
    q.pop();
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if ((i == 0 && j == 0) || (i != 0 && j != 0)) continue;
        int xx = x + i;
        int yy = y + j;
        if (xx >= 0 && xx < h && yy >= 0 && yy < w && !vis[xx][yy] &&
            grid[xx][yy] != '#') {
          ans = min(ans, foo(xx, yy) + 1);
          vis[xx][yy] = 1;
          if (cur.dis < k) q.push({xx, yy, cur.dis + 1});
        }
      }
    }
  }
}
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);
}
