#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char mp[1005][1005];
bool visited[1005][1005];
void work(int x, int y) {
  visited[x][y] = true;
  int tx, ty;
  tx = x - 1, ty = y;
  if (mp[tx][ty] == '.' && !visited[tx][ty]) work(tx, ty);
  tx = x + 1, ty = y;
  if (mp[tx][ty] == '.' && !visited[tx][ty]) work(tx, ty);
  tx = x, ty = y - 1;
  if (mp[tx][ty] == '.' && !visited[tx][ty]) work(tx, ty);
  tx = x, ty = y + 1;
  if (mp[tx][ty] == '.' && !visited[tx][ty]) work(tx, ty);
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) scanf("\n%s", mp[i] + 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (mp[i][j] == 'S') work(i, j);
  int ans = 1e9;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (!visited[i][j]) continue;
      int tmp;
      tmp = i - 1;
      if (tmp == 0)
        ans = 0;
      else
        ans = min(ans, (tmp - 1) / k + 1);
      tmp = j - 1;
      if (tmp == 0)
        ans = 0;
      else
        ans = min(ans, (tmp - 1) / k + 1);
      tmp = n - i;
      if (tmp == 0)
        ans = 0;
      else
        ans = min(ans, (tmp - 1) / k + 1);
      tmp = m - j;
      if (tmp == 0)
        ans = 0;
      else
        ans = min(ans, (tmp - 1) / k + 1);
    }
  cout << ans + 1 << endl;
  return 0;
}
