#include <bits/stdc++.h>
using namespace std;
int h, w, k;
string s[810];
queue<pair<int, int> > q;
bool vis[810][810];
int len[810][810];
int minf(int a, int b, int c, int d) { return min(a, min(b, min(c, d))); }
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int sx, sy;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  s[sx][sy] = '.';
  q.push(make_pair(sx, sy));
  int dist = 1e9;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    vis[x][y] = 1;
    int d = minf(x, h - x - 1, y, w - y - 1);
    dist = min(dist, d);
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i], yy = y + dy[i];
      if (xx >= 0 && xx < h && yy > 0 && yy <= w && s[xx][yy] == '.' &&
          vis[xx][yy] == 0 && len[xx][yy] <= k) {
        len[xx][yy] = len[x][y] + 1;
        q.push(make_pair(xx, yy));
      }
    }
  }
  cout << (dist + k - 1) / k + 1;
  return 0;
}
