#include <bits/stdc++.h>
using namespace std;
long long h, w, k;
string grid[810];
bool vis[810][810];
long long ans = INT_MAX;
long long movey[4] = {0, 0, -1, 1};
long long movex[4] = {-1, 1, 0, 0};
long long magic(long long y, long long x) {
  queue<vector<long long> > wow;
  wow.push({y, x, 0});
  while (!wow.empty()) {
    long long nowy = wow.front()[0];
    long long nowx = wow.front()[1];
    long long nowsteps = wow.front()[2];
    wow.pop();
    if (vis[nowy][nowx] || grid[nowy][nowx] == '#') continue;
    vis[nowy][nowx] = 1;
    long long add = 1 + min(nowy, min(nowx, min(h - nowy - 1, w - nowx - 1)));
    if (nowy == 0 || nowx == 0 || nowy == h - 1 || nowx == w - 1) add = 0;
    ans = min(ans, add + nowsteps / k + (nowsteps % k > 0));
    for (long long i = 0; i < 4; i++) {
      long long targety = nowy + movey[i];
      long long targetx = nowx + movex[i];
      if (targety < 0 || targetx < 0 || targety > h - 1 || targetx > w - 1)
        continue;
      if (vis[targety][targetx] || grid[targety][targetx] == '#') continue;
      wow.push({targety, targetx, nowsteps + 1});
    }
  }
  return INT_MAX;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long starty = -1;
  long long startx = -1;
  cin >> h >> w >> k;
  for (long long i = 0; i < h; i++) {
    cin >> grid[i];
    if (starty >= 0) continue;
    for (long long j = 0; j < w; j++) {
      if (grid[i][j] == 'S') {
        starty = i;
        startx = j;
        break;
      }
    }
  }
  magic(starty, startx);
  cout << ans << endl;
}
