#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};
void DFS(int x, int 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] == '.')
      DFS(nx, ny);
  }
}
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') DFS(i, j);
  cout << ans << endl;
  return 0;
}
