#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using P = pair<int, int>;
using P3 = pair<P, ll>;
using PP = pair<P, P>;
constexpr int INF = 1 << 30;
constexpr ll MOD = ll(1e9) + 7;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr double EPS = 1e-9;
int h, w, k;
vector<vector<int> > d;
bool isIn(int i, int j) { return i >= 0 && i < h && j >= 0 && j < w; }
int solve() {
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  d.resize(h, vector<int>(w, INF));
  queue<P3> que[2], nxtque[2];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        que[0].push(P3(P(i, j), k));
        d[i][j] = 1;
      }
    }
  }
  int ans = INF;
  while (1) {
    while (!que[0].empty()) {
      auto tmp = que[0].front();
      que[0].pop();
      int i = tmp.first.first, j = tmp.first.second, r = tmp.second;
      for (int l = 0; l < 4; l++) {
        int ni = i + di[l], nj = j + dj[l];
        if (r > 0) {
          if (isIn(ni, nj)) {
            if (s[ni][nj] == '.' && d[ni][nj] > d[i][j]) {
              d[ni][nj] = d[i][j];
              que[0].push(P3(P(ni, nj), r - 1));
            } else if (s[ni][nj] == '#' && d[ni][nj] > d[i][j]) {
              d[ni][nj] = d[i][j];
              que[1].push(P3(P(ni, nj), k - 1));
            }
          } else {
            cout << d[i][j] << endl;
            return 0;
          }
        } else {
          if (isIn(ni, nj)) {
            if (s[ni][nj] == '.' && d[ni][nj] > d[i][j] + 1) {
              d[ni][nj] = d[i][j] + 1;
              nxtque[0].push(P3(P(ni, nj), k));
            } else if (s[ni][nj] == '#' && d[ni][nj] > d[i][j]) {
              d[ni][nj] = d[i][j];
              que[1].push(P3(P(ni, nj), k - 1));
            }
          } else {
            cout << d[i][j] + 1 << endl;
            return 0;
          }
        }
      }
    }
    while (!que[1].empty()) {
      auto tmp = que[1].front();
      que[1].pop();
      int i = tmp.first.first, j = tmp.first.second, r = tmp.second;
      for (int l = 0; l < 4; l++) {
        int ni = i + di[l], nj = j + dj[l];
        if (r > 0) {
          if (isIn(ni, nj)) {
            if (s[ni][nj] == '.' && d[ni][nj] > d[i][j] + 1) {
              d[ni][nj] = d[i][j] + 1;
              nxtque[0].push(P3(P(ni, nj), k));
            } else if (s[ni][nj] == '#' && d[ni][nj] > d[i][j]) {
              d[ni][nj] = d[i][j];
              que[1].push(P3(P(ni, nj), r - 1));
            }
          } else {
            cout << d[i][j] + 1 << endl;
            return 0;
          }
        } else {
          if (isIn(ni, nj)) {
            if (s[ni][nj] == '.' && d[ni][nj] > d[i][j] + 1) {
              d[ni][nj] = d[i][j] + 1;
              nxtque[0].push(P3(P(ni, nj), k));
            } else if (s[ni][nj] == '#' && d[ni][nj] > d[i][j] + 1) {
              d[ni][nj] = d[i][j] + 1;
              nxtque[1].push(P3(P(ni, nj), k - 1));
            }
          } else {
            cout << d[i][j] + 1 << endl;
            return 0;
          }
        }
      }
    }
    que[0] = nxtque[0];
    que[1] = nxtque[1];
    while (!nxtque[0].empty()) nxtque[0].pop();
    while (!nxtque[1].empty()) nxtque[1].pop();
  }
  assert(false);
}
int main() {
  solve();
  return 0;
}
