#include <bits/stdc++.h>
using namespace std;
int h, w, k, flag = 0;
int res = 0xffffff;
int si, sj, ni, nj;
int graph[805][805];
int vis[805][805];
int dis(int i, int j) {
  int c1 = ceil((i - 1) * 1.0 / k);
  int c2 = ceil((j - 1) * 1.0 / k);
  int c3 = ceil((h - i) * 1.0 / k);
  int c4 = ceil((w - j) * 1.0 / k);
  return min(min(c1, c2), min(c3, c4));
}
void dfs(int i, int j, int s) {
  if (flag) return;
  if (s < 0) return;
  if (graph[i][j] || vis[i][j]) return;
  if (i == 1 || j == 1 || i == h || j == w) {
    flag = 1;
    res = 0;
    return;
  }
  if (res >= dis(i, j)) {
    res = dis(i, j);
    ni = i;
    nj = j;
  }
  vis[i][j] = 1;
  dfs(i + 1, j, s - 1);
  dfs(i - 1, j, s - 1);
  dfs(i, j + 1, s - 1);
  dfs(i, j - 1, s - 1);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> h >> w >> k;
  char line[w + 5];
  for (int i = 1; i <= h; i++) {
    cin >> line;
    for (int j = 1; j <= w; j++) {
      if (line[j - 1] == '#')
        graph[i][j] = 1;
      else if (line[j - 1] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  dfs(si, sj, k);
  cout << res + 1 << endl;
  return 0;
}
