#include <bits/stdc++.h>
using namespace std;
int main() {
  char s[800][800];
  int w, h, k, ss, ss1, sx1, sz1, sy1, sx, sz, sy;
  cin >> h >> w >> k;
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      cin >> s[i][j];
      if (s[i][j] == 'S') {
        ss = i;
        ss1 = j;
        sx = w - i - 1;
        sx1 = j;
        sz = j;
        sz1 = i;
        sy = h - 1 - j;
        sy1 = i;
      }
    }
  }
  int ans = 0;
  int j = 0, d = 0;
  for (int i = ss - 1; i >= 0; i++) {
    if (s[i][ss1] == '#') {
      j++;
    } else
      d++;
  }
  d -= k;
  ans += (j + k - 1) / k;
  ans++;
  d = ss - k;
  if (d >= 0) {
    ans += (d + k - 1) / k;
  }
  int ans1;
  for (int i = h - sx; i < h; i++) {
    if (s[i][sx1] == '#') {
      j++;
    } else
      d++;
  }
  d -= k;
  ans1 += (j + k - 1) / k;
  ans1++;
  d = sx - k;
  if (d >= 0) {
    ans1 += (d + k - 1) / k;
  }
  ans = min(ans, ans1);
  ans1 = 0;
  for (int i = 0; i < sz; i++) {
    if (s[sz1][i] == '#') {
      j++;
    } else
      d++;
  }
  d -= k;
  ans1 += (j + k - 1) / k;
  ans1++;
  d = sz - k;
  if (d >= 0) {
    ans1 += (d + k - 1) / k;
  }
  ans = min(ans, ans1);
  for (int i = w - sy; i < w; i++) {
    if (s[sy1][i] == '#') {
      j++;
    } else
      d++;
  }
  d -= k;
  ans1 += (j + k - 1) / k;
  ans1++;
  d = sy - k;
  if (d >= 0) {
    ans1 += (d + k - 1) / k;
  }
  ans = min(ans, ans1);
  cout << ans;
  return 0;
}
