#include <bits/stdc++.h>
using namespace std;
int h, w, k;
int a[810][810];
int b[810][810];
string d;
int sx, sy;
queue<pair<int, int> > q;
int ans;
const int dx[]{0, 0, 1, -1};
const int dy[]{1, -1, 0, 0};
void bfs(int xx, int yy, int step) {
  int newstep = min(min(xx - 1, yy + 1), min(h - xx, w - yy));
  newstep = newstep % k == 0 ? newstep / k : newstep / k + 1;
  ans = min(ans, 1 + newstep);
  if (step >= k) return;
  for (int i = 0; i < 4; i++) {
    if (a[xx + dx[i]][yy + dy[i]] == '.' && !b[xx + dx[i]][yy + dy[i]]) {
      b[xx + dx[i]][yy + dy[i]] = 1;
      bfs(xx + dx[i], yy + dy[i], step + 1);
    }
  }
}
int main() {
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    cin >> d;
    for (int o = 1; o <= w; o++) {
      a[i][o] = d[o - 1];
      if (a[i][o] == 'S') sx = i, sy = o;
    }
  }
  ans = 0x7fffffff;
  bfs(sx, sy, 0);
  cout << ans;
}
