#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char a[810][810];
int dd[] = {1, 0, -1, 0, 1};
int si, sj;
bool valid(int i, int j) { return i >= 0 && i < h && j >= 0 && j < w; }
int main() {
  cin.sync_with_stdio(false);
  cin >> h >> w >> k;
  for (long long i = (long long)(0); i < (long long)(h); i++) {
    cin >> a[i];
    for (long long j = (long long)(0); j < (long long)(w); j++)
      if (a[i][j] == 'S') {
        si = i;
        sj = j;
        a[i][j] = '.';
      }
  }
  int res = INT_MAX / 3;
  for (long long d = (long long)(0); d < (long long)(4); d++) {
    int sum = 1;
    int pi = si, pj = sj;
    while (1) {
      for (int cnt = 0; cnt < k; pi += dd[d], pj += dd[d + 1], cnt++) {
        if (!valid(pi, pj)) break;
        if (a[pi][pj] == '#') {
          pi -= dd[d];
          pj -= dd[d + 1];
          break;
        }
      }
      if (!valid(pi, pj)) break;
      for (int ni = si, nj = sj, cnt = 0; cnt < k;
           ni += dd[d], nj += dd[d + 1]) {
        if (!valid(ni, nj)) break;
        if (a[ni][nj] == '#') {
          a[ni][nj] = '.';
          cnt++;
        }
      }
      sum++;
    }
    res = (res < sum ? res : sum);
  }
  cout << res << endl;
  return 0;
}
