#include <bits/stdc++.h>
using namespace std;
char a[801][801];
queue<pair<int, int> > p[640000];
int x, y, k;
int z;
int c, d;
void tansaku(int q) {
  for (int i = 0; i < p[q].size(); i++) {
    int xx = p[q].front().first;
    int yy = p[q].front().second;
    if (xx > 0) {
      if (a[xx - 1][yy] == '.') {
        a[xx - 1][yy] = 'V';
        p[q - 1].push(make_pair(xx - 1, yy));
      }
    }
    if (xx < x - 1) {
      if (a[xx + 1][yy] == '.') {
        a[xx + 1][yy] = 'V';
        p[q - 1].push(make_pair(xx + 1, yy));
      }
    }
    if (yy > 0) {
      if (a[xx][yy - 1] == '.') {
        a[xx][yy - 1] = 'V';
        p[q - 1].push(make_pair(xx, yy - 1));
      }
    }
    if (yy < y - 1) {
      if (a[xx][yy + 1] == '.') {
        a[xx][yy + 1] = 'V';
        p[q - 1].push(make_pair(xx, yy + 1));
      }
    }
    p[q].pop();
  }
  if (q > 0) tansaku(q - 1);
  return;
}
int main(void) {
  scanf("%d %d %d", &x, &y, &k);
  for (int i = 0; i < x; i++) {
    scanf("%s", a[i]);
  }
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      if (a[i][j] == 'S') {
        a[i][j] = 'V';
        p[k - 1].push(make_pair(i, j));
        tansaku(k - 1);
      }
    }
  }
  d = 1000;
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      if (a[i][j] == 'V') {
        c = i;
        if (x - i - 1 < c) c = x - i - 1;
        if (j < c) c = j;
        if (y - j - 1 < c) c = y - j - 1;
        if (c < d) d = c;
      }
    }
  }
  if (d == 0)
    z = 1;
  else {
    z = (d - 1) / k;
    z = z + 2;
  }
  printf("%d\n", z);
  return 0;
}
