#include <bits/stdc++.h>
using namespace std;
int n, m, k, mark[1005][1005], basx, basy,
    b[2][4] = {{1, -1, 0, 0}, {0, 0, 1, -1}}, mini = 1000000;
char arr[1005][1005];
queue<pair<int, pair<int, int>>> q, qq;
vector<pair<int, int>> v;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> arr[i][j];
      if (arr[i][j] == 'S') {
        basx = j;
        basy = i;
      }
    }
  }
  pair<int, pair<int, int>> a;
  a.first = 0;
  a.second.first = basy;
  a.second.second = basx;
  q.push(a);
  while (!q.empty()) {
    a = q.front();
    q.pop();
    int cost = a.first;
    int y = a.second.first;
    int x = a.second.second;
    if (mark[y][x] == 1 || x < 1 || y < 1 || x > m || y > n || arr[y][x] == '#')
      continue;
    mark[y][x] = 1;
    v.push_back({y, x});
    for (int i = 0; i < 4; i++) q.push({cost + 1, {y + b[0][i], x + b[1][i]}});
  }
  for (auto i : v) {
    qq.push({0, i});
  }
  memset(mark, 0, sizeof mark);
  while (!qq.empty()) {
    a = qq.front();
    qq.pop();
    int cost = a.first;
    int y = a.second.first;
    int x = a.second.second;
    if (mark[y][x] == 1 || x < 1 || y < 1 || x > m || y > n) continue;
    mark[y][x] = 1;
    if (x == 1 || y == 1 || x == m || y == n) {
      mini = min(mini, cost / k + (cost % k != 0) + 1);
    }
    for (int i = 0; i < 4; i++) qq.push({cost + 1, {y + b[0][i], x + b[1][i]}});
  }
  cout << mini;
}
