#include <bits/stdc++.h>
using namespace std;
char s[808][808];
struct node {
  int x, y, step;
} q, p;
int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
int main() {
  queue<node> que;
  int n, m, x, y, k;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int k = 0; k < m; k++) {
      if (s[i][k] == 'S') {
        x = i;
        y = k;
      }
    }
  }
  int ans = 0x7f7f7f7f;
  que.push({x, y, 0});
  s[x][y] = '#';
  while (!que.empty()) {
    q = que.front();
    x = q.x, y = q.y;
    if (q.step < k && (x == 0 || x == n - 1 || y == 0 || y == m - 1)) ans = 0;
    ans = min(ans, (x + k - 1) / k);
    ans = min(ans, (y + k - 1) / k);
    ans = min(ans, (n - x + k - 1) / k);
    ans = min(ans, (m - y + k - 1) / k);
    que.pop();
    for (int i = 0; i < 4; i++) {
      p.x = x + dir[i][0], p.y = y + dir[i][1], p.step = q.step + 1;
      if (p.x >= 0 && p.x < n && p.y >= 0 && p.y < m && p.step <= k &&
          s[p.x][p.y] == '.') {
        que.push(p);
        s[p.x][p.y] = '#';
      }
    }
  }
  cout << ans + 1 << endl;
}
