#include <bits/stdc++.h>
using namespace std;
string s[1000];
int visited[1000][1000];
long long n, m, k;
long long x, y, ans;
long long dp[4][1000][1000];
void dfs(long long i, long long j) {
  visited[i][j] = 1;
  if (!visited[i - 1][j] && s[i - 1][j] != '#' && i - 1 >= 0) dfs(i - 1, j);
  if (!visited[i + 1][j] && s[i + 1][j] != '#' && i + 1 <= n) dfs(i + 1, j);
  if (!visited[i][j - 1] && s[i][j - 1] != '#' && j - 1 >= 0) dfs(i, j - 1);
  if (!visited[i][j + 1] && s[i][j + 1] != '#' && j + 1 <= n) dfs(i, j + 1);
  ans = min(dp[0][i][j], min(dp[1][i][j], min(dp[2][i][j], dp[3][i][i])));
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'S') {
        x = i;
        y = j;
      }
    }
  }
  for (int i = 1; i < n - 1; i++) {
    for (int j = 1; j < m - 1; j++) {
      dp[0][i][j] = dp[0][i - 1][j];
      dp[2][i][j] = dp[2][i][j - 1];
      if (s[i - 1][j] == '#') dp[0][i][j]++;
      if (s[i][j - 1] == '#') dp[2][i][j]++;
    }
  }
  for (int i = n - 2; i > 0; i--) {
    for (int j = n - 2; j > 0; j--) {
      dp[1][i][j] = dp[1][i + 1][j];
      dp[3][i][j] = dp[3][i][j + 1];
      if (s[i + 1][j] == '#') dp[1][i][j]++;
      if (s[i][j + 1] == '#') dp[3][i][j]++;
    }
  }
  dfs(x, y);
  cout << ceil(ans / (float)k) + 1;
  return 0;
}
