#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;
int check(int i, int j) {
  return (min(min(i, h - 1 - i), min(j, w - 1 - j)) + k - 1) / k + 1;
}
void solve(int i, int j, int k) {
  if (i == 0 || j == 0 || i == h - 1 || j == w - 1) {
    ans = 1;
    return;
  }
  ans = min(ans, check(i, j));
  vis[i][j] = 1;
  if (k == 0) return;
  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])
          solve(i + di, j + dj, 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') solve(i, j, k);
  cout << ans << endl;
}
