#include <bits/stdc++.h>
using namespace std;
const int maxn = 805;
char s[maxn][maxn];
int bio[maxn][maxn];
int pr[4] = {1, 0, -1, 0};
int ps[4] = {0, 1, 0, -1};
int brred, brstup;
int vani(int r, int c) { return r < 0 || c < 0 || r >= brred || c >= brstup; }
void bfs(int r, int c) {
  for (int i = 0; i < maxn; ++i)
    for (int j = 0; j < maxn; ++j) bio[i][j] = 1e9;
  queue<pair<int, int> > Q;
  Q.push(pair<int, int>(r, c));
  bio[r][c] = 0;
  for (; !Q.empty();) {
    pair<int, int> tmp = Q.front();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      int nr = tmp.first + pr[i];
      int nc = tmp.second + ps[i];
      if (!vani(nr, nc) && s[nr][nc] != '#' &&
          bio[nr][nc] > bio[tmp.first][tmp.second] + 1) {
        bio[nr][nc] = bio[tmp.first][tmp.second] + 1;
        Q.push(pair<int, int>(nc, nc));
      }
    }
  }
}
int main(int argc, char const *argv[]) {
  int K;
  cin >> brred >> brstup >> K;
  pair<int, int> poc = pair<int, int>(-1, -1);
  for (int i = 0; i < brred; ++i) {
    scanf(" %s", s[i]);
    for (int j = 0; j < brstup; ++j) {
      if (s[i][j] == 'S') {
        poc = pair<int, int>(i, j);
      }
    }
  }
  bfs(poc.first, poc.second);
  int rje = 1e9;
  for (int i = 0; i < brred; ++i) {
    for (int j = 0; j < brstup; ++j) {
      if (bio[i][j] <= K) {
        int mn = min(min(i, j), min(brred - i - 1, brstup - j - 1));
        int jos = (mn + K - 1) / K;
        rje = min(rje, 1 + jos);
      }
    }
  }
  cout << rje << endl;
  return 0;
}
