#include <bits/stdc++.h>
using namespace std;
int h, w, k;
char mp[1000][1000];
int be1, be2;
int ans = 1e9;
int main() {
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> mp[i][j];
      if (mp[i][j] == 'S') be1 = i, be2 = j;
    }
  int cnt = 0, move = 0, unlock = 0;
  for (int i = be1 - 1; i >= 0; i--) {
    int res = 0;
    move++;
    unlock += (mp[i][be2] == '#' ? 1 : 0);
    if (unlock > cnt * k) res++;
    if (move > cnt * k) res++;
    cnt += res;
  }
  ans = min(ans, cnt);
  cnt = 0, move = 0, unlock = 0;
  for (int i = be1 + 1; i < h; i++) {
    int res = 0;
    move++;
    unlock += (mp[i][be2] == '#' ? 1 : 0);
    if (unlock > cnt * k) res++;
    if (move > cnt * k) res++;
    cnt += res;
  }
  ans = min(ans, cnt);
  cnt = 0, move = 0, unlock = 0;
  for (int i = be2 - 1; i >= 0; i--) {
    int res = 0;
    move++;
    unlock += (mp[be1][i] == '#' ? 1 : 0);
    if (unlock > cnt * k) res++;
    if (move > cnt * k) res++;
    cnt += res;
  }
  ans = min(ans, cnt);
  cnt = 0, move = 0, unlock = 0;
  for (int i = be2 + 1; i <= w; i++) {
    int res = 0;
    move++;
    unlock += (mp[be1][i] == '#' ? 1 : 0);
    if (unlock > cnt * k) res++;
    if (move > cnt * k) res++;
    cnt += res;
  }
  ans = min(ans, cnt);
  cout << ans << endl;
  return 0;
}
