#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int date = 0, w = 1;
  char c = 0;
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    date = date * 10 + c - '0';
    c = getchar();
  }
  return date * w;
}
void chkmin(int &x, int y) { x = min(x, y); }
void chkmax(int &x, int y) { x = max(x, y); }
const int Q = 1000;
int n, m, k, a[Q][Q], ct;
char tmp;
pair<int, int> st, lis[Q * Q], l2[Q * Q];
int cnt;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int vis[Q][Q];
struct node {
  int x, y, len;
};
void bfs() {
  queue<node> q;
  int x = st.first;
  int y = st.second;
  node tp;
  tp.x = x;
  tp.y = y;
  tp.len = 0;
  q.push(tp);
  vis[x][y] = 1;
  l2[++ct] = make_pair(x, y);
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y, len = q.front().len;
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int tx = x + dx[i], ty = y + dy[i];
      if (a[tx][ty] || vis[tx][ty] || len == k) {
        continue;
      }
      if (tx < 2 || tx >= n || ty < 2 || ty >= n) {
        cout << 1 << endl;
        exit(0);
      }
      tp.x = x;
      tp.y = y;
      tp.len = len + 1;
      q.push(tp);
      vis[tx][ty] = 1;
      l2[++ct] = make_pair(tx, ty);
    }
  }
}
int cal(int p) {
  int x = l2[p].first, y = l2[p].second;
  int A, B, C, D;
  if ((x - 1) % k == 0)
    A = (x - 1) / k;
  else
    A = (x - 1) / k + 1;
  if ((n - x) % k == 0)
    B = (n - x) / k;
  else
    B = (n - x) / k + 1;
  if ((y - 1) % k == 0)
    C = (y - 1) / k;
  else
    C = (y - 1) / k + 1;
  if ((m - y) % k == 0)
    B = (m - y) / k;
  else
    B = (m - y) / k + 1;
  return min(A, min(B, min(C, D)));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> tmp;
      if (tmp == 'S') {
        st = make_pair(i, j);
      } else if (tmp == '#') {
        a[i][j] = 1;
      }
    }
  }
  bfs();
  int ans = INT_MAX;
  for (int i = 1; i <= ct; ++i) {
    chkmin(ans, cal(i));
  }
  cout << ans + 1 << endl;
  return 0;
}
