#include <bits/stdc++.h>
using namespace std;
int dirx[] = {1, -1, 0, 0};
int diry[] = {0, 0, 1, -1};
class agcC {
 private:
  int H, W, K;
  bool isvalid(int x, int y) { return (x >= 0 && x < H && y >= 0 && y < W); }

 public:
  void solve(istream& in, ostream& out) {
    in >> H >> W >> K;
    vector<vector<int>> A(H, vector<int>(W));
    int sx = -1, sy = -1;
    for (int i = 0; i < H; ++i) {
      for (int j = 0; j < W; ++j) {
        char c;
        in >> c;
        A[i][j] = (c == '#' ? 1 : 0);
        if (c == 'S') {
          sx = i;
          sy = j;
        }
      }
    }
    assert(sx != -1 && sy != -1);
    queue<tuple<int, int, int>> q, tq;
    vector<vector<int>> vis(H, vector<int>(W, 0));
    tq.emplace(sx, sy, K);
    while (!tq.empty()) {
      int cx, cy, ck;
      tie(cx, cy, ck) = tq.front();
      tq.pop();
      q.emplace(cx, cy, 1);
      if (cx == 0 || cx == H || cy == 0 || cy == W) {
        out << "1\n";
        return;
      }
      vis[cx][cy] = 1;
      for (int i = 0; ck && i < 4; ++i) {
        int xx = cx + dirx[i], yy = cy + diry[i];
        if (isvalid(xx, yy) && !A[xx][yy] && !vis[xx][yy]) {
          tq.emplace(xx, yy, ck - 1);
        }
      }
    }
    vis.assign(H, vector<int>(W, 0));
    while (!q.empty()) {
      int cx, cy, cc;
      tie(cx, cy, cc) = q.front();
      q.pop();
      for (int i = 0; i < 4; ++i) {
        int xx = cx + K * dirx[i], yy = cy + K * diry[i];
        if (xx > 0 && xx < H && yy > 0 && yy < W) {
          q.emplace(xx, yy, cc + 1);
        } else {
          out << cc + 1 << "\n";
          return;
        }
      }
    }
    assert(false);
  }
};
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  agcC solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
