#include <bits/stdc++.h>
using namespace std;
const int inf = 999999999;
const double pi = acos(-1);
int h, w, k;
char d[808][808];
int si, sj;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ans = inf;
int main() {
  cin >> h >> w >> k;
  for (int i = (0); i < (int)(h); i++)
    for (int j = (0); j < (int)(w); j++) {
      cin >> d[i][j];
      if (d[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  for (int l = (0); l < (int)(4); l++) {
    int i = si, j = sj;
    int cnt = 0;
    while (1) {
      for (int f = (0); f < (int)(k); f++) {
        if ((0 <= i + dy[l] && i + dy[l] < h && 0 <= j + dx[l] &&
             j + dx[l] < w) &&
            d[i + dy[l]][j + dx[l]] == '.') {
          i += dy[l];
          j += dx[l];
        } else {
          break;
        }
      }
      int c = 0, ii = i, jj = j;
      while (c < k) {
        if (0 <= ii + dy[l] && ii + dy[l] < h && 0 <= jj + dx[l] &&
            jj + dx[l] < w) {
          ii += dy[l];
          jj += dx[l];
          if (d[ii][jj] == '#') {
            d[ii][jj] = '.';
            c++;
          }
        } else {
          break;
        }
      }
      cnt++;
      if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
        break;
      }
    }
    ans = min(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}
