#include <bits/stdc++.h>
using namespace std;
char mp[805][805];
int n, m, k;
int ans = 1e9;
int xx[4] = {-1, 0, 1, 0}, yy[4] = {0, -1, 0, 1};
struct msg {
  int x, y, step;
};
queue<msg> q;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (mp[i][j] == 'S') q.push((msg){i, j, 0});
  while (!q.empty()) {
    msg v = q.front();
    q.pop();
    if (v.step > k) continue;
    int x = v.x, y = v.y;
    mp[x][y] = 'x';
    ans = min(ans, (min(min(x - 1, n - x), min(y - 1, m - y)) + k - 1) / k + 1);
    for (int i = 0; i < 4; i++) {
      int nx = x + xx[i], ny = y + yy[i];
      if (1 <= nx && nx <= n && 1 <= ny && ny <= m && mp[nx][ny] == '.')
        q.push((msg){nx, ny, v.step + 1});
    }
  }
  cout << ans << endl;
  return 0;
}
