#include <bits/stdc++.h>
using namespace std;
class UnionFind {
  vector<int> data;

 public:
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};
int solve(int H, int W, int K, vector<string>& a) {
  vector<vector<int>> d(H, vector<int>(W));
  for (int i = 0, j = H - 1; i <= j; ++i, --j) {
    int k = (i + K - 1) / K;
    for (int c = 0; c < (W); ++c) d[i][c] = d[j][c] = k;
  }
  for (int i = 0, j = W - 1; i <= j; ++i, --j) {
    int k = (i + K - 1) / K;
    for (int r = 0; r < (H); ++r) {
      d[r][i] = min(d[r][i], k);
      d[r][j] = min(d[r][j], k);
    }
  }
  int start = -1;
  UnionFind uf(H * W);
  for (int i = 0; i < (H); ++i)
    for (int j = 0; j < (W); ++j) {
      if (a[i][j] == '#') continue;
      int here = (W * (i) + (j));
      if (a[i][j] == 'S') start = here;
      if (0 < i && a[i - 1][j] == '.') uf.unionSet((W * (i - 1) + (j)), here);
      if (i < H - 1 && a[i + 1][j] == '.')
        uf.unionSet((W * (i + 1) + (j)), here);
      if (0 < j && a[i][j - 1] == '.') uf.unionSet((W * (i) + (j - 1)), here);
      if (j < W - 1 && a[i][j + 1] == '.')
        uf.unionSet((W * (i) + (j + 1)), here);
    }
  int root0 = uf.root(start);
  int best = INT_MAX;
  for (int i = 0; i < (H); ++i)
    for (int j = 0; j < (W); ++j) {
      if (uf.root((W * (i) + (j))) == root0) {
        best = min(best, d[i][j]);
      }
    }
  return 1 + best;
}
int main() {
  int H, W, K;
  cin >> H >> W >> K;
  vector<string> a(H);
  for (int i = 0; i < (H); ++i) cin >> a[i];
  cout << solve(H, W, K, a) << endl;
  return 0;
}
