#include <bits/stdc++.h>
using namespace std;
char grid[801][801];
int sx, sy;
int best;
int n, m, k;
pair<int, int> v[801][801];
vector<pair<pair<int, int>, int> > ok;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
void bfs() {
  queue<int> q;
  q.push(sx), q.push(sy);
  v[sx][sy] = make_pair(1, 0);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int y = q.front();
    q.pop();
    if (x == 0 || y == 0 || x == n - 1 || y == m - 1) {
      best = min(best, v[x][y].second + (v[x][y].first + k - 2) / k);
    }
    for (int i = 0; i < 4; i++) {
      int nx = dx[i] + x, ny = dy[i] + y;
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || v[nx][ny].first) continue;
      v[nx][ny] = v[x][y];
      if (grid[nx][ny] && v[nx][ny].first - 1 <= k) v[nx][ny].second = 1;
      q.push(nx);
      q.push(ny);
    }
  }
}
int main() {
  best = (int)1e9;
  cin >> n >> m >> k;
  getchar();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      grid[i][j] = getchar();
      if (grid[i][j] == 'S') sx = i, sy = j;
    }
    getchar();
  }
  bfs();
  cout << best << endl;
  return 0;
}
