#include <bits/stdc++.h>
using namespace std;
int H, W, K, u, d, l, r, t, sh, sw;
char a[800][800];
int MIN(int a, int b, int c, int d) { return min(min(a, b), min(c, d)); }
int main() {
  scanf("%d %d %d\n", &H, &W, &K);
  for (int i = 0; i < H - 1; i++) {
    scanf("%s\n", a[i]);
    for (int j = 0; j < W; j++)
      if (a[i][j] == 'S') sh = i, sw = j;
  }
  t = 0, u = sh / K + 1;
  for (int j = sw; j >= 0; j--)
    if (a[sh][j] == '#') t++;
  if (sh % K == 0) u--;
  if (t >= K) u++;
  t = 0, d = (H - 1 - sh) / K + 1;
  for (int j = sw; j < W; j++)
    if (a[sh][j] == '#') t++;
  if ((H - 1 - sh) % K == 0) d--;
  if (t >= K) d++;
  t = 0, l = sw / K + 1;
  for (int i = sh; i >= 0; i--)
    if (a[i][sw] == '#') t++;
  if (sh % K == 0) l--;
  if (t >= K) l++;
  t = 0, r = (W - 1 - sw) / K + 1;
  for (int i = sh; i < H; i++)
    if (a[i][sw] == '#') t++;
  if ((W - 1 - sw) % K == 0) r--;
  if (t >= K) r++;
  printf("%d\n", MIN(u, d, l, r));
  return 0;
}
