#include <bits/stdc++.h>
using namespace std;
const int N = 8e3 + 5;
int n, m, k;
pair<int, int> st;
bool vis[N][N];
char c[N][N];
int wx[4] = {0, 0, -1, 1};
int wy[4] = {1, -1, 0, 0};
vector<pair<int, int> > vec;
queue<pair<int, pair<int, int> > > Q;
void dfs(int x, int y) {
  if (vis[x][y] == true || c[x][y] == '#' || x < 1 || y < 1 || x > n || y > m)
    return;
  vec.push_back({x, y});
  vis[x][y] = true;
  for (int i = 0; i < 4; i++) dfs(x + wx[i], y + wy[i]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> c[i][j];
      if (c[i][j] == 'S') st = {i, j};
    }
  Q.push({0, st});
  while (Q.empty() == false) {
    auto temp = Q.front();
    Q.pop();
    int cost = temp.first, x = temp.second.first, y = temp.second.second;
    if (vis[x][y] == true || cost > k || x < 1 || y < 1 || x > n || y > m)
      continue;
    vec.push_back({x, y});
    vis[x][y] = true;
    for (int i = 0; i < 4; i++) Q.push({cost + 1, {x + wx[i], y + wy[i]}});
  }
  while (Q.empty() == false) Q.pop();
  int ans = 1e9;
  for (auto i : vec) Q.push({0, i});
  memset(vis, 0, sizeof vis);
  while (Q.empty() == false) {
    auto temp = Q.front();
    Q.pop();
    int cost = temp.first, x = temp.second.first, y = temp.second.second;
    if (vis[x][y] == true || x < 1 || y < 1 || x > n || y > m) continue;
    vis[x][y] = true;
    if (x == 1 || x == n || y == 1 || y == m)
      ans = min(ans, cost / k + (cost % k != 0) + 1);
    for (int i = 0; i < 4; i++) Q.push({cost + 1, {x + wx[i], y + wy[i]}});
  }
  cout << ans << endl;
}
