#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char s[832][332];
bool f;
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'S') {
        f = 1;
        int disu = (i + 1 + k - 1) / k;
        if (s[i - 1][j] == '#') disu++;
        int disd = (n - i - 1 + k - 1) / k;
        if (s[i + 1][j] == '#') disd++;
        int disl = (j + 1 + k - 1) / k;
        if (s[i][j - 1] == '#') disl++;
        int disr = (n - j - 1 + k - 1) / k;
        if (s[i][j + 1] == '#') disr++;
        int ans = min(min(disu, disd), min(disl, disr));
        cout << ans << endl;
        break;
      }
    }
    if (f) break;
  }
  return 0;
}
