#include <bits/stdc++.h>
using namespace std;
int minn = 1000000, xt, yt, ans, n, m, k, maxx;
char a[802][802];
bool vis[802][802];
int wei[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int pan(int x, int y) {
  int l1 = x - 1, l2 = n - x, l3 = m - y, l4 = y - 1;
  return (min(min(l1, l2), min(l3, l4)));
}
void dfs(int x, int y, int cen) {
  if (cen > k) return;
  if (pan(x, y) < minn) minn = pan(x, y);
  vis[x][y] = 1;
  if (a[x + 1][y] == '.' && !vis[x + 1][y]) dfs(x + 1, y, cen + 1);
  if (a[x][y + 1] == '.' && !vis[x][y + 1]) dfs(x, y + 1, cen + 1);
  if (a[x][y - 1] == '.' && !vis[x][y - 1]) dfs(x, y - 1, cen + 1);
  if (a[x - 1][y] == '.' && !vis[x - 1][y]) dfs(x - 1, y, cen + 1);
}
int main() {
  cin >> n >> m >> k;
  memset(vis, 1, sizeof(vis));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) vis[i][j] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') xt = i, yt = j;
    }
  if (pan(xt, yt) == 0) {
    cout << 0;
    return 0;
  }
  vis[xt][yt] = 1;
  dfs(xt, yt, 0);
  ans = 1;
  if ((minn / k) * k == minn)
    ans += minn / k;
  else
    ans += minn / k + 1;
  cout << ans;
}
