#include <bits/stdc++.h>
using namespace std;
int H, W, K;
bool r[810][810];
int d[810][810];
int e[5] = {1, 0, -1, 0, 1};
int ans = 1e9 + 7;
queue<pair<int, int> > Q;
int main() {
  scanf("%d%d%d", &H, &W, &K);
  for (int i = (int)(1); i < (int)(H + 1); ++i) {
    for (int j = (int)(1); j < (int)(W + 1); ++j) {
      char c;
      d[i][j] = -1;
      scanf(" %c", &c);
      if (c == 'S') {
        r[i][j] = 1;
        d[i][j] = 0;
        Q.push(make_pair((i), (j)));
      } else if (c == '#') {
        r[i][j] = 0;
      } else {
        r[i][j] = 1;
      }
    }
  }
  while (!Q.empty()) {
    int x = Q.front().first;
    int y = Q.front().second;
    Q.pop();
    if (x == 1 || x == H || y == 1 || y == W) {
      ans = min(ans, d[x][y]);
    } else {
      for (int i = (int)(0); i < (int)(4); ++i) {
        int u = x + e[i];
        int v = y + e[i + 1];
        if (d[u][v] >= 0) continue;
        if (r[u][v]) {
          d[u][v] = d[x][y] + 1;
        } else if (d[x][y] % K == 0 || d[x][y] <= K) {
          d[u][v] = K + 1;
        } else {
          d[u][v] = d[x][y] + 1;
        }
        Q.push(make_pair((u), (v)));
      }
    }
  }
  printf("%d\n", (ans + K - 1) / K);
  return 0;
}
