#include <bits/stdc++.h>
using namespace std;
long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long h, w, k;
  cin >> h >> w >> k;
  char mp[h][w];
  pair<long long, long long> mpp[h][w];
  long long ans[h][w];
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      ans[i][j] = 0;
    }
  }
  long long ANS = 1000000010;
  queue<pair<long long, long long> > bfs;
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      cin >> mp[i][j];
      if (mp[i][j] == 'S') {
        bfs.push(make_pair(i, j));
        mpp[i][j] = make_pair(k, 0);
        ans[i][j] = 1;
      }
    }
  }
  while (!bfs.empty()) {
    pair<long long, long long> p = bfs.front();
    bfs.pop();
    for (long long i = 0; i < 4; i++) {
      long long X = p.first + dx[i], Y = p.second + dy[i];
      if (X >= 0 && X < h && Y >= 0 && Y < w) {
        if (ans[X][Y] == 0) {
          if (mp[X][Y] == '.') {
            if (mpp[p.first][p.second].first != 0) {
              mpp[X][Y].first = mpp[p.first][p.second].first - 1;
              mpp[X][Y].second = mpp[p.first][p.second].second;
              ans[X][Y] = ans[p.first][p.second];
              bfs.push(make_pair(X, Y));
            } else {
              mpp[X][Y].first = k - 1;
              mpp[X][Y].second = mpp[p.first][p.second].second + k;
              ans[X][Y] = ans[p.first][p.second] + 1;
              bfs.push(make_pair(X, Y));
            }
          } else if (mp[X][Y] == '#') {
            if (mpp[p.first][p.second].second != 0) {
              if (mpp[p.first][p.second].first != 0) {
                mpp[X][Y].first = mpp[p.first][p.second].first - 1;
                mpp[X][Y].second = mpp[p.first][p.second].second - 1;
                ans[X][Y] = ans[p.first][p.second];
                bfs.push(make_pair(X, Y));
              } else {
                mpp[X][Y].first = k - 1;
                mpp[X][Y].second = mpp[p.first][p.second].second + k - 1;
                ans[X][Y] = ans[p.first][p.second] + 1;
                bfs.push(make_pair(X, Y));
              }
            } else {
              mpp[X][Y].first = k - 1;
              mpp[X][Y].second = mpp[p.first][p.second].second + k - 1;
              ans[X][Y] = ans[p.first][p.second] + 1;
              bfs.push(make_pair(X, Y));
            }
          }
        }
      }
    }
  }
  for (long long i = 0; i < h; i++) {
    ANS = min(ANS, min(ans[i][0], ans[i][w - 1]));
  }
  for (long long i = 0; i < w; i++) {
    ANS = min(ANS, min(ans[0][i], ans[h - 1][i]));
  }
  cout << ANS << "\n";
  return 0;
}
