#include <bits/stdc++.h>
using namespace std;
int h, w, k, x, y, ans;
char A[(long long)810][(long long)810], s[(long long)810];
char d[(long long)810 * (long long)810], e[(long long)810 * (long long)810];
void dfs(int a, int b, int l) {
  if (a == 1 || b == 1 || a == h || b == w) {
    for (int i = 1; i <= l; i++) {
      e[i] = d[i];
    }
    int sum = 0, pos = 0;
    while (1) {
      if (pos > l) break;
      sum++;
      int start = pos;
      while (1) {
        pos++;
        if (d[pos] == '#' || (pos - start) > k || pos > l) break;
      }
      if (pos <= l)
        pos--;
      else
        break;
      start = pos;
      int cnt = 0;
      while (1) {
        pos++;
        if (cnt > k || pos > l) break;
        if (d[pos] == '#') {
          d[pos] = '.';
          cnt++;
        }
      }
      pos = start;
    }
    ans = min(ans, sum);
    for (int i = 1; i <= l; i++) d[i] = e[i];
    return;
  }
  d[l + 1] = A[a - 1][b];
  dfs(a - 1, b, l + 1);
  d[l + 1] = A[a + 1][b];
  dfs(a + 1, b, l + 1);
  d[l + 1] = A[a][b - 1];
  dfs(a, b - 1, l + 1);
  d[l + 1] = A[a][b + 1];
  dfs(a, b + 1, l + 1);
}
int main() {
  scanf("%d %d %d", &h, &w, &k);
  for (int i = 1; i <= h; i++) {
    scanf("%s", s);
    for (int j = 1; j <= w; j++) {
      A[i][j] = s[j - 1];
      if (A[i][j] == 'S') {
        x = i;
        y = j;
        A[i][j] = '.';
      }
    }
  }
  ans = 1 << 30;
  dfs(x, y, 0);
  printf("%d\n", ans);
  return 0;
}
