#include <bits/stdc++.h>
using namespace std;
const int N = 1050;
int n, m, ans = N;
bool vis[N][N];
char mp[N][N];
void update(int x, int y) {
  vis[x][y] = 1;
  ans = min(ans, y);
  ans = min(ans, m - 1 - y);
  ans = min(ans, x);
  ans = min(ans, n - 1 - x);
}
void dfs(int x, int y, int k) {
  if (k < 0 || x < 0 || x >= n || y < 0 || y >= m || mp[x][y] == '#' ||
      vis[x][y] == 1)
    return;
  update(x, y);
  dfs(x - 1, y, k - 1);
  dfs(x + 1, y, k - 1);
  dfs(x, y - 1, k - 1);
  dfs(x, y + 1, k - 1);
}
int main() {
  int k, x, y;
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) scanf("%s", mp[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (mp[i][j] == 'S') {
        x = i;
        y = j;
        break;
      }
  dfs(x, y, k);
  if (ans)
    printf("%d\n", (ans - 1) / k + 2);
  else
    printf("1\n");
  return 0;
}
