#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  int H, W, K;
  scanf("%d%d%d", &H, &W, &K);
  char maze[H + 2][W + 2];
  int dist[H + 2][W + 2];
  for (int i = 0; i < int(H + 2); i++) {
    dist[i][0] = dist[i][W + 1] = 9999;
  }
  for (int i = 0; i < int(W + 2); i++) {
    dist[0][i] = dist[H + 1][i] = 9999;
  }
  pair<int, int> start;
  for (int i = int(1); i < int(H + 1); i++) {
    for (int j = int(1); j < int(W + 1); j++) {
      char a;
      scanf(" %c", &a);
      maze[i][j] = a;
      if (a == 'S') {
        dist[i][j] = 0;
        start.first = i;
        start.second = j;
      } else
        dist[i][j] = 9999;
    }
  }
  int up, low, left, right;
  up = low = start.first;
  left = right = start.second;
  for (int k = int(1); k < int(K + 1); k++) {
    for (int i = int(1); i < int(H + 1); i++) {
      for (int j = int(1); j < int(W + 1); j++) {
        if (maze[i][j] == '.' && dist[i][j] > k &&
            (dist[i - 1][j] == k - 1 || dist[i + 1][j] == k - 1 ||
             dist[i][j - 1] == k - 1 || dist[i][j + 1] == k - 1)) {
          dist[i][j] = k;
          if (i < up) up = i;
          if (i > low) low = i;
          if (j < left) left = j;
          if (j > right) right = j;
        }
      }
    }
  }
  int tmp = min(min(low - 1, H - up), min(left - 1, W - right));
  int ans = (tmp + 2 * K - 1) / K;
  printf("%d\n", ans);
  return 0;
}
