#include <bits/stdc++.h>
using namespace std;
int n, m;
const int maxn = 805;
string s[maxn];
int dis[maxn][maxn];
int K;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
const int INF = 0x3f3f3f3f;
bool in(int x, int y) { return x >= 0 and x < n and y >= 0 and y < m; }
void bfs(vector<pair<int, int> > st) {
  memset(dis, 0x3f, sizeof(dis));
  queue<pair<int, int> > q;
  for (auto v : st) {
    q.push(v);
    dis[v.first][v.second] = 0;
  }
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int x = p.first;
    int y = p.second;
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k];
      int ny = y + dy[k];
      if (in(nx, ny)) {
        if (s[nx][ny] != '#' and dis[nx][ny] > dis[x][y] + 1) {
          dis[nx][ny] = dis[x][y] + 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m >> K;
  int sx, sy;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'S') {
        sx = i, sy = j;
      }
    }
  }
  bfs({make_pair(sx, sy)});
  for (int i = 0; i < n; i++) {
    if (dis[i][0] != INF or dis[i][m - 1] != INF) {
      cout << 1 << endl;
      return 0;
    }
  }
  for (int j = 0; j < m; j++) {
    if (dis[0][j] != INF or dis[n - 1][j] != INF) {
      cout << 1 << endl;
      return 0;
    }
  }
  int ans = INF;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (dis[i][j] <= K) {
        ans = min(ans, min(min(i, n - 1 - i), min(j, m - 1 - j)));
      }
    }
  }
  cout << (ans + K - 1) / K + 1 << endl;
  return 0;
}
