#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char c[805][805];
int sx, sy;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
bool vis[805][805];
int ans;
void bfs(int x, int y, int step) {
  priority_queue<pair<int, pair<int, int> > > q;
  q.push(make_pair(0, make_pair(x, y)));
  while (!q.empty()) {
    pair<int, pair<int, int> > t = q.top();
    q.pop();
    int tx = t.second.first;
    int ty = t.second.second;
    int ln = -t.first;
    if (ln > k) {
      break;
    }
    for (int i = 0; i < 4; i++) {
      int nx = tx + dx[i];
      int ny = ty + dy[i];
      if (vis[nx][ny] == 0 && c[nx][ny] != '#' && nx > 0 && nx <= n && ny > 0 &&
          ny <= m) {
        ans =
            min(1 + (min(min(nx - 1, ny - 1), min(n - nx, m - ny)) + k - 1) / k,
                ans);
        q.push(make_pair(-(ln + 1), make_pair(nx, ny)));
        vis[nx][ny] = true;
      }
    }
  }
}
int main() {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  vis[sx][sy] = true;
  ans = 1 + (min(min(sx - 1, sy - 1), min(n - sx, m - sy)) + k - 1) / k;
  bfs(sx, sy, 0);
  cout << ans;
  return 0;
}
