#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char map[805][805];
int dis[805][805];
bool flag = true;
void dps(int si, int sj) {
  queue<int> q;
  q.push(si);
  q.push(sj);
  q.push(0);
  while (!q.empty()) {
    int i = q.front();
    q.pop();
    int j = q.front();
    q.pop();
    int n = q.front();
    q.pop();
    if (n < k) {
      if (map[i + 1][j] == '.') {
        map[i + 1][j] = 'S';
        q.push(i + 1);
        q.push(j);
        q.push(n + 1);
      } else if (map[i][j + 1] == '.') {
        map[i][j + 1] = 'S';
        q.push(i);
        q.push(j + 1);
        q.push(n + 1);
      } else if (map[i - 1][j] == '.') {
        map[i - 1][j] = 'S';
        q.push(i - 1);
        q.push(j);
        q.push(n + 1);
      } else if (map[i][j - 1] == '.') {
        map[i][j - 1] = 'S';
        q.push(i);
        q.push(j - 1);
        q.push(n + 1);
      }
    }
  }
}
int main() {
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) scanf("%s", &map[i][1]);
  int si = -1, sj = -1;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      if (map[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  dps(si, sj);
  int min = 10000;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      if (map[i][j] == 'S') {
        if (min > i - 1) min = i - 1;
        if (min > j - 1) min = j - 1;
        if (min > w - j) min = w - j;
        if (min > h - i) min = h - i;
      }
    }
  printf("%d", (min + k - 1) / k + 1);
  return 0;
}
