#include<stdio.h>
int main(void){
  char *s="python3 -c\""
"def solve():\n"
"    from collections import deque\n"
"    h, w, k = map(int, input().split())\n"
"    maze = [input() for _ in range(h)]\n"
"    visited = [[-1]*w for _ in range(h)]\n"
"    for i in range(h):\n"
"        if 'S' in maze[i]:\n"
"            sy, sx = i, maze[i].index('S')\n"
"            break\n"
"    queue = deque([[sy, sx]])\n"
"    visited[sy][sx] = 0\n"
"    r = 10**18\n"
"    while queue:\n"
"        y, x = queue.popleft()\n"
"        r = min(r, y, h-1-y, x, w-1-x)\n"
"        if visited[y][x] == k: continue\n"
"        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):\n"
"            new_y, new_x = y+i, x+j\n"
"            if new_y < 0 or new_y >= h or new_x < 0 or new_x >= w: continue\n"
"            if maze[new_y][new_x] == '.' and visited[new_y][new_x] == -1:\n"
"                visited[new_y][new_x] = visited[y][x] + 1\n"
"                queue.append([new_y, new_x])\n"
"    print(1 -(-r // k))\n"
"if __name__ == '__main__':\n"
"    solve()\n"
"\"";
  system(s);
  return 0;
}            