;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil nil t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(defstruct (queue (:constructor make-queue
                    (&optional list &aux (tail (last list)))))
  (list nil :type list)
  (tail nil :type (or null (cons t null))))

(declaim (inline enqueue))
(defun enqueue (obj queue)
  "Pushes OBJ to the end of QUEUE."
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (setf (cdr tail) (list obj)
              tail (cdr tail))))
  queue)

(declaim (inline dequeue))
(defun dequeue (queue)
  "Removes and returns the element at the front of QUEUE. Returns NIL if QUEUE
is empty."
  (pop (queue-list queue)))

(declaim (inline queue-empty-p))
(defun queue-empty-p (queue)
  (null (queue-list queue)))

(declaim (inline enqueue-front))
(defun enqueue-front (obj queue)
  "Pushes OBJ to the front of QUEUE."
  (symbol-macrolet ((list (queue-list queue))
                    (tail (queue-tail queue)))
    (if (null list)
        (setf tail (list obj)
              list tail)
        (push obj list))
    queue))

(declaim (inline read-schar))
(defun read-schar (&optional (stream *standard-input*))
  (declare #-swank (sb-kernel:ansi-stream stream)
           (inline read-byte))
  #+swank (read-char stream nil #\Newline) ; on SLIME
  #-swank (code-char (read-byte stream nil #.(char-code #\Newline))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defconstant +inf+ #xffff)
(defun main ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (k (read))
         (plan (make-array (list h w) :element-type 'bit :initial-element 0))
         (que (make-queue))
         (dists (make-array (list h w) :element-type 'uint16 :initial-element +inf+)))
    (declare (uint31 h w k))
    (dotimes (i h)
      (dotimes (j w)
        (case (read-schar)
          (#\# (setf (aref plan i j) 1))
          (#\S
           (enqueue (list* i j 0) que)
           (setf (aref dists i j) 0))))
      (read-schar))
    (labels ((visit (i j new-dist)
               (declare (int32 i j new-dist))
               (when (and (<= 0 i (- h 1))
                          (<= 0 j (- w 1))
                          (= +inf+ (aref dists i j))
                          (zerop (aref plan i j)))
                 (setf (aref dists i j) 0)
                 (enqueue (list* i j new-dist) que))))
      (loop until (queue-empty-p que)
            for (i j . dist) of-type (uint31 uint31 . uint31) = (dequeue que)
            when (< dist k)
            do (visit (- i 1) j (+ dist 1))
               (visit (+ i 1) j (+ dist 1))
               (visit i (- j 1) (+ dist 1))
               (visit i (+ j 1) (+ dist 1)))
      (labels ((visit (i j new-dist)
                 (declare (int32 i j new-dist))
                 (when (and (<= 0 i (- h 1))
                            (<= 0 j (- w 1))
                            (= +inf+ (aref dists i j)))
                   (setf (aref dists i j) new-dist)
                   (enqueue (cons i j) que))))
        (dotimes (i h)
          (dotimes (j w)
            (when (zerop (aref dists i j))
              (enqueue (cons i j) que))))
        (loop until (queue-empty-p que)
              for (i . j) = (dequeue que)
              for dist = (aref dists i j)
              do (visit (- i 1) j (+ dist 1))
                 (visit (+ i 1) j (+ dist 1))
                 (visit i (- j 1) (+ dist 1))
                 (visit i (+ j 1) (+ dist 1)))
        (println
         (+ 1
            (ceiling (min (loop for i below h
                                minimize (min (aref dists i 0)
                                              (aref dists i (- w 1))))
                          (loop for j below w
                                minimize (min (aref dists 0 j)
                                              (aref dists (- h 1) j))))
                     k)))))))

#-swank (main)


Measured memory_bytes: 94820