#include <bits/stdc++.h>
void update(std::vector<std::string> v,
            std::vector<std::vector<long long> > &cost, long long x,
            long long y, long long k, bool first = false) {
  for (int i = -k; i <= k; i++) {
    for (int j = -(k - std::abs(i)); j <= k - std::abs(i); j++) {
      if (x + i < 0 || x + i >= cost.size() || y + j < 0 ||
          y + j >= cost[0].size()) {
        continue;
      }
      if (first && v[x + i][y + j] == '#') {
        continue;
      }
      cost[x + i][y + j] = std::min(cost[x + i][y + j], cost[x][y] + 1);
    }
  }
}
int main() {
  long long h, w, k, sx, sy;
  std::cin >> h >> w >> k;
  std::vector<std::string> v(h);
  for (long long i = 0; i < h; i++) {
    std::cin >> v[i];
    auto pos = std::find(v[i].begin(), v[i].end(), 'S');
    if (pos != v[i].end()) {
      sx = i;
      sy = pos - v[i].begin();
    }
  }
  std::vector<std::vector<long long> > cost(h,
                                            std::vector<long long>(w, 1000000));
  cost[sx][sy] = 0;
  std::stack<std::pair<long long, long long> > s;
  update(v, cost, sx, sy, k, true);
  if (v[sx - 1][sy] == '#' && v[sx + 1][sy] == '#' && v[sx][sy - 1] == '#' &&
      v[sx][sy + 1] == '#') {
    cost[sx][sy] = 1;
  }
  for (int i = 0; i < 500; i++) {
    for (int j = 0; j < h; j++) {
      for (int l = 0; l < w; l++) {
        if (cost[j][l] == i) {
          update(v, cost, j, l, k);
        }
      }
    }
  }
  long long ans = 10000000000;
  for (int i = 0; i < h; i++) {
    ans = std::min(ans, cost[i][0]);
    ans = std::min(ans, cost[i][w - 1]);
  }
  for (int i = 0; i < w; i++) {
    ans = std::min(ans, cost[0][i]);
    ans = std::min(ans, cost[h - 1][i]);
  }
  std::cout << ans << std::endl;
  return 0;
}
