(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro buffered-read-line (&optional (buffer-size 30) (in '*standard-input*) (terminate-char #\Space))
  "Note that the returned string will be reused."
  (let ((buffer (gensym))
        (character (gensym))
        (idx (gensym)))
    `(let ((,buffer (load-time-value (make-string ,buffer-size
                                                  :element-type 'base-char))))
       (declare (simple-base-string ,buffer))
       (loop for ,character of-type base-char =
                #-swank (code-char (read-byte ,in nil #\Newline))
                #+swank (read-char ,in nil #\Newline)
             for ,idx from 0
             until (char= ,character #\Newline)
             do (setf (schar ,buffer ,idx) ,character)
             finally (setf (schar ,buffer ,idx) ,terminate-char)
                     (return (values ,buffer ,idx))))))

(declaim (inline split-integers-into-array))
(defun split-integers-into-array (string dest-array row &key (offset 0))
  (declare (string string)
           ((simple-array * (* *)) dest-array)
           ((integer 0 #.most-positive-fixnum) row offset))
  (loop for idx from offset below (array-dimension dest-array 1)
        for pos1 = 0 then (1+ pos2)
        for pos2 = (position #\space string :start pos1 :test #'char=)
        do (setf (aref dest-array row idx)
                 (parse-integer string :start pos1 :end pos2))
        finally (return dest-array)))

(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)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

;; Hauptteil

(defun main ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (costs (make-array '(10 10) :element-type 'uint16))
         (wall (make-array (list h w) :element-type 'int16)))
    (declare (uint15 h w))
    (dotimes (i 10)
      (split-integers-into-array (buffered-read-line 80) costs i))
    (dotimes (i h)
      (split-integers-into-array (buffered-read-line 800) wall i))
    (dotimes (k 10)
      (dotimes (i 10)
        (dotimes (j 10)
          (when (> (aref costs i j) (+ (aref costs i k) (aref costs k j)))
            (setf (aref costs i j) (+ (aref costs i k) (aref costs k j)))))))
    (println
     (loop for i below h
           sum (loop for j below w
                     sum (if (= -1 (aref wall i j))
                             0
                             (aref costs (aref wall i j) 1))
                     of-type uint32)
           of-type uint32))))

#-swank(main)


Measured memory_bytes: 25952