#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pair<int, int> > vp;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define out(n) cout << (n) << endl;

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int h, w, k, minw, mini, z;
vector<vector<char> > buil(801, vector<char>(801));
vvi ma(801, vi(801, 99999));

int tansaku(int gh, int gw, int num) {
  int mmini = min(min(gh, h - gh), min(gw, w - gw));
  if(ma[gh][gw] <= num || buil[gh][gw] == '#' || mini + (k - num) < mmini) {
    return 1;
  }

  if(gh == 0 || gh == h - 1 || gw == 0 || gw == w - 1) {
    return 0;
  }

  if(num == k) {
    int mh = min(gh, h - gh), mw = min(gw, w - gw);
    int res = min(mh, mw);
    if(mini > res) {
      mini = res;
      z = gh <= mw && gh <= h - gh ? 0
      : h - gh <= mw && h - gh <= gh ? 1
      : gw <= mh && gw <= w - gw ? 2 : 3;
    }
    return 1;
  }

  ma[gh][gw] = num;

  return min(
    min(tansaku(gh + 1, gw, num + 1),
    tansaku(gh - 1, gw, num + 1)),
    min(tansaku(gh, gw + 1, num + 1),
    tansaku(gh, gw - 1, num + 1))
  );
}

int main() {
  cin >> h >> w >> k;

  char a;
  int sh, sw;

  rep(i, 0, h) {
    rep(j, 0, w) {
      cin >> a;
      if(a == 'S') {
        sh = i;
        sw = j;
      }

      buil[i][j] = a;
    }
  }

  int mh = min(sh, h - sh), mw = min(sw, w - sw);
  mini = min(mh, mw);

  z = sh <= mw && sh <= h - sh ? 0
  : h - sh <= sw && h - sh <= sh ? 1
  : sw <= sh && sw <= w - sw ? 2 : 3;

  if(!tansaku(sh, sw, 0)) {
    out(1);
    return 0;
  }

  out((z == 0 ? mini : ) / k + 2)

  out(mini / k + 2);
}
