#include <bits/stdc++.h>
using namespace std;
const int MAXN = 800 + 50;
char f[MAXN + 1][MAXN + 1];
int d[MAXN + 1][MAXN + 1], n, m, k,
    mv[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}, calc;
bool valid(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= m; }
bool win(int x, int y) { return (x == 1) || (x == n) || (y == 1) || (y == m); }
void dfs(int x, int y, int can) {
  if (can == 0) return;
  for (int i = 0; i < 4; i++) {
    int dx = x + mv[i][0];
    int dy = y + mv[i][1];
    if (valid(dx, dy) && d[dx][dy] == -1) {
      if (f[dx][dy] == '#') f[dx][dy] = '.';
      dfs(dx, dy, can - 1);
      calc++;
    }
  }
}
int main() {
  cin >> n >> m >> k;
  int a = -1, b = -1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> f[i][j];
      if (f[i][j] == 'S') a = i, b = j;
      d[i][j] = -1;
    }
  }
  queue<pair<int, int> > q;
  q.push({a, b});
  d[a][b] = 0;
  int cur = k, ans = 1;
  vector<pair<int, int> > was;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    if (win(x, y)) {
      cout << ans << "\n";
      return 0;
    }
    was.push_back({x, y});
    q.pop();
    if (d[x][y] == cur) {
      for (auto it : was) {
        calc = 0;
        dfs(it.first, it.second, k);
        if (calc > 0) q.push({it.first, it.second});
      }
      was.clear();
      cur += k;
      ans++;
    }
    for (int i = 0; i < 4; i++) {
      int dx = x + mv[i][0];
      int dy = y + mv[i][1];
      if (valid(dx, dy) && d[dx][dy] == -1 && f[dx][dy] != '#') {
        d[dx][dy] = d[x][y] + 1;
        q.push({dx, dy});
      }
    }
    if (q.empty()) {
      for (auto it : was) {
        calc = 0;
        dfs(it.first, it.second, k);
        if (calc > 0) q.push({it.first, it.second});
      }
      was.clear();
      cur += k;
      ans++;
    }
  }
  cout << ans << "\n";
  return 0;
}
