#include <bits/stdc++.h>
using namespace std;
string mapa[808];
int visited[808][808];
int dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int n, m, k;
bool isok(int x, int y) {
  return x >= 0 && x < n && y >= 0 && y < m && mapa[x][y] != '#' &&
         visited[x][y] == -1;
}
int main() {
  memset(visited, -1, sizeof(visited));
  cin >> n >> m >> k;
  for (int ctr1 = 0; ctr1 < n; ctr1++) cin >> mapa[ctr1];
  queue<pair<int, int> > bfs;
  for (int ctr1 = 0; ctr1 < n; ctr1++)
    for (int ctr2 = 0; ctr2 < m; ctr2++) {
      if (mapa[ctr1][ctr2] == 'S')
        bfs.push(make_pair(ctr1, ctr2)), visited[ctr1][ctr2] = 0;
    }
  while (!bfs.empty()) {
    pair<int, int> cur = bfs.front();
    bfs.pop();
    for (int ctr1 = 0; ctr1 < 4; ctr1++) {
      pair<int, int> nxt =
          make_pair(cur.first + dir[ctr1][0], cur.second + dir[ctr1][1]);
      if (isok(nxt.first, nxt.second)) {
        visited[nxt.first][nxt.second] = visited[cur.first][cur.second] + 1;
        bfs.push(nxt);
      }
    }
  }
  int rez = 1000;
  for (int ctr1 = 0; ctr1 < n; ctr1++) {
    for (int ctr2 = 0; ctr2 < m; ctr2++) {
      if (visited[ctr1][ctr2] < k && visited[ctr1][ctr2] != -1) {
        rez = min(rez,
                  1 + (min(min(ctr1, n - 1 - ctr1), min(ctr2, m - 1 - ctr1)) +
                       k - 1) /
                          k);
      }
    }
  }
  cout << rez;
  return 0;
}
