#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
char c[880][880];
int n, m, k, stx, sty;
int ans = 0x7fffffff;
bool used[886][886];
inline int dist(int x, int y) {
  int ans = min(min(n - x - 1, m - y - 1), min(x, y));
  if (ans % k)
    return ans / k + 1;
  else
    return ans / k;
}
inline bool inr(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
inline bool win(int x, int y) {
  return x == 0 || y == 0 || x == n - 1 || y == m - 1;
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    scanf("%s", &c[i]);
    for (int j = 0; j < m; j++)
      if (c[i][j] == 'S') stx = i, sty = j;
  }
  queue<pair<int, int> > q;
  used[stx][sty] = true;
  q.push(make_pair(stx, sty));
  int I_Hate_Atcoder = 1;
  while (!q.empty() && I_Hate_Atcoder <= k) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (!inr(nx, ny) || used[nx][ny] || c[nx][ny] == '#') continue;
      used[nx][ny] = true;
      if (win(x, y)) {
        cout << 1;
        return 0;
      }
      q.push(make_pair(nx, ny));
    }
    I_Hate_Atcoder++;
  }
  int mn = 0x7fffffff;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (used[i][j]) mn = min(mn, dist(i, j));
    }
  cout << mn + 1 << endl;
  return 0;
}
