#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", 3, "inline")
using namespace std;
string s[805];
bool vis[1000][1000];
int ans = 0x7f7f7f7f;
int n, m, k;
int sx, sy;
int aford[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
bool check(int x, int y) {
  if (x < 0 || x >= n || y < 0 || y >= m) return 0;
  if (vis[x][y] || s[x][y] == '#') return 0;
  vis[x][y] = 1;
  return 1;
}
void road(int x, int y) {
  int num = min(min(n - 1 - x, x), min(m - 1 - y, y));
  ans = min(ans, num);
}
void dfs(int step, int x, int y) {
  if (step == k) return;
  road(x, y);
  vis[x][y] = 1;
  for (int i = 0; i < 4; i++)
    if (check(x + aford[i][0], y + aford[i][1]))
      dfs(step + 1, x + aford[i][0], y + aford[i][1]);
}
int main() {
  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') {
        sx = i, sy = j;
        break;
      }
    }
  }
  dfs(0, sx, sy);
  cout << (ans + k - 1) / k + 1;
  return 0;
}
