#include <bits/stdc++.h>
using namespace std;

#define int long long
#define inf 1000000007LL

#define rep(i, n) for(int i = 0; i < (n); i++)
#define rrep(i, n) for(int i = (n) - 1; i >= 0; i--)
#define trep(i, n) for(int i = 0; i <= (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define mfor(i, s, t) for(int i = (s); i < (t); i++)
#define tfor(i, s, t) for(int i = (s); i <= (t); i++)

class Dinic {
public:
  vector<vector<pair<int, int>>> v;
  vector<int> e;

  vector<int> k;
  vector<int> j;

  void init(int n) {
    v = vector<vector<pair<int, int>>>(n);
    e = vector<int>();
    k = vector<int>(n);
    j = vector<int>(n);
  }
  void add_edge(int a, int b, int atob, int btoa = 0) {
    v[a].push_back(make_pair(b, e.size()));
    e.push_back(atob);
    v[b].push_back(make_pair(a, e.size()));
    e.push_back(btoa);
  }
  void dyk(int s) {
    for(auto& i : k) {
      i = inf;
    }
    queue<pair<int, int>> pq;
    pq.push(make_pair(0, s));
    while(!pq.empty()) {
      int w = pq.front().first;
      int b = pq.front().second;
      pq.pop();
      if(k[b] > w) {
        k[b] = w;
        for(auto i : v[b]) {
          if(e[i.second] > 0) {
            pq.push(make_pair(w + 1, i.first));
          }
        }
      }
    }
  }
  int dfs(int s, int t, int p, int c) {
    if(p == t) {
      return c;
    }
    int all = 0;
    while(c > all && j[p] < v[p].size()) {
      if(k[v[p][j[p]].first] == k[p] + 1) {
        int r = dfs(s, t, v[p][j[p]].first, min(c - all, e[v[p][j[p]].second]));
        e[v[p][j[p]].second] -= r;
        e[v[p][j[p]].second ^ 1] += r;
        if(r < c - all) {
          j[p]++;
        }
        all += r;
      }
      else {
        j[p]++;
      }
    }
    return all;
  }
  int solve(int s, int t) {
    int all = 0;
    while(1) {
      dyk(s);
      for(auto& i : j) {
        i = 0;
      }
      int r = dfs(s, t, s, inf);
      if(r == 0) {
        return all;
      }
      all += r;
    }
  }
};

class Rmq {
public:
  vector<int> d;
  int n;

  void init(int nw) {
    n = 1;
    while(n < nw) {
      n *= 2;
    }
    d.resize(n * 2);
    rep(i, n * 2) {
      d[i] = inf * inf;
    }
  }
  void set_(int b, int x, int p, int nb, int ne) {
    if(nb <= b && b < ne) {
      if(nb == ne - 1) {
        d[p] = min(d[p], x);
      }
      else {
        int m = (nb + ne) / 2;
        set_(b, x, p * 2 + 1, nb, m);
        set_(b, x, p * 2 + 2, m, ne);
        d[p] = min(d[p * 2 + 1], d[p * 2 + 2]);
      }
    }
  }
  void set(int b, int x) {
    set_(b, x, 0, 0, n);
  }
  int get_(int b, int e, int p, int nb, int ne) {
    if(b <= nb && ne <= e) {
      return d[p];
    }
    else if(ne <= b || e <= nb) {
      return inf * inf;
    }
    else {
      int m = (nb + ne) / 2;
      return min(get_(b, e, p * 2 + 1, nb, m), get_(b, e, p * 2 + 2, m, ne));
    }
  }
  int get(int b, int e) {
    return get_(b, e, 0, 0, n);
  }
};

string s[1919];

int d[1919][1919];

int dy[4] = {0, 1, 0, -1};
int dx[4] = {1, 0, -1, 0};

signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  rep(i, h) {
    cin >> s[i];
  }
  queue<pair<int, pair<int, int>>> pq;
  rep(i, h) {
    rep(j, w) {
      if(s[i][j] == 'S') {
        pq.push(make_pair(0, make_pair(i, j)));
      }
      d[i][j] = inf;
    }
  }
  int ans = inf;
  while(!pq.empty()) {
    int a = pq.front().first;
    pair<int, int> b = pq.front().second;
    pq.pop();
    if(0 <= b.first && b.first < h && 0 <= b.second && b.second < w && s[b.first][b.second] != '#' && d[b.first][b.second] > a && a <= k) {
      d[b.first][b.second] = a;
      ans = min(ans, (min(min(b.first, h - 1 - b.first), min(b.second, w - 1 - b.second)) + k - 1) / k + 1);
      rep(i, 4) {
        pq.push(make_pair(a + 1, make_pair(b.first + dy[i], b.second + dx[i])));
      }
    }
  }
  cout << ans << endl;
}


Measured memory_bytes: 12800