#include <bits/stdc++.h>
using namespace std;
const int N = 805;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int used[N][N], dist[N][N];
int h, w, k;
char grid[N][N];
queue<pair<int, int>> q;
int go(int x, int y) {
  dist[x][y] = 0;
  used[x][y] = 1;
  q.push({x, y});
  int d;
  int t = 0;
  int ret = 1 << 30;
  int xx = x, yy = y;
  while (!q.empty() && t < k) {
    auto ft = q.front();
    q.pop();
    x = ft.first, y = ft.second;
    if (x == 0 || x == h - 1 || y == 0 || y == w - 1) return 1;
    for (int k = 0; k < 4; k++) {
      int xk = x + dx[k];
      int yk = y + dy[k];
      if (xk < 0 || xk >= h || yk < 0 || yk >= w) continue;
      if (used[xk][yk]) continue;
      if (grid[xk][yk] == '#') continue;
      q.push({xk, yk});
    }
    t++;
  }
  q.push({xx, yy});
  while (!q.empty()) {
    auto ft = q.front();
    q.pop();
    x = ft.first = y = ft.second;
    ret = min(ret, (x + k - 1) / k + 1);
    ret = min(ret, (y + k - 1) / k + 1);
    ret = min(ret, (h - x + k - 2) / k + 1);
    ret = min(ret, (w - y + k - 2) / k + 1);
  }
  return ret;
}
int man(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); }
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 0; i < h; i++) scanf("%s", grid[i]);
  int ans = 1 << 30;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (grid[i][j] == 'S') {
        int ans = go(i, j);
        printf("%d\n", ans);
        return 0;
      }
  return 0;
}
