#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 dist[N][N];
int h, w, k;
char grid[N][N];
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
               greater<pair<int, pair<int, int>>>>
    pq;
void go(int x, int y) {
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) dist[i][j] = 1 << 30;
  dist[x][y] = 0;
  pq.push({0, {x, y}});
  int d;
  while (!pq.empty()) {
    auto v = pq.top();
    pq.pop();
    d = v.first, x = v.second.first, y = v.second.second;
    if (d > dist[x][y]) continue;
    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 (grid[xk][yk] == '#') {
        if (dist[xk][yk] > dist[x][y] + 1) {
          dist[xk][yk] = dist[x][y] + 1;
          pq.push({dist[xk][yk], {xk, yk}});
        }
      } else {
        if (dist[xk][yk] > dist[x][y]) {
          dist[xk][yk] = dist[x][y];
          pq.push({dist[xk][yk], {xk, yk}});
        }
      }
    }
  }
}
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') {
        go(i, j);
        for (int i = 0; i < h; i++) {
          ans = min(ans, dist[i][0] / k + (dist[i][0] % k > 0) + 1);
          ans = min(ans, dist[i][w - 1] / k + (dist[i][w - 1] % k > 0) + 1);
        }
        for (int i = 0; i < w; i++) {
          ans = min(ans, dist[0][i] / k + (dist[0][i] % k > 0) + 1);
          ans = min(ans, dist[h - 1][i] / k + (dist[h - 1][i] % k > 0) + 1);
        }
        printf("%d\n", ans);
        return 0;
      }
  return 0;
}
