#include <bits/stdc++.h>
using namespace std;
int h, w, k;
const int INF = 1e9;
const int N = 850;
string A[N];
int vis[N][N];
int ans = INF;
queue<int> Q;
int check(int i, int j) {
  return ans = min(ans,
                   (min(min(i, h - 1 - i), min(j, w - 1 - j)) + k - 1) / k + 1);
}
int main() {
  cin >> h >> w >> k;
  for (int i = 0; i < h; ++i) cin >> A[i];
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      if (A[i][j] == 'S') Q.push(i), Q.push(j), Q.push(k), vis[i][j] = 1;
  while (!Q.empty()) {
    int i = Q.front();
    Q.pop();
    int j = Q.front();
    Q.pop();
    int k = Q.front();
    Q.pop();
    if (check(i, j) == 1) break;
    if (k == 0) break;
    for (int di = -1; di <= 1; ++di)
      for (int dj = -1; dj <= 1; ++dj)
        if (abs(di) + abs(dj) == 1)
          if (A[i + di][j + dj] == '.' && !vis[i + di][j + dj])
            Q.push(di + i), Q.push(dj + j), Q.push(k - 1),
                vis[i + di][j + dj] = 1;
  }
  cout << ans << endl;
}
