;; -*- coding: utf-8 -*-
(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 (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
;;;
;;; Max flow (Dinic's algorithm)
;;;

(setf *print-circle* t)

(defconstant +graph-max-distance+ #xffffffff)

(define-condition max-flow-overflow (simple-error)
  ((graph :initarg :graph :reader max-flow-overflow-graph))
  (:report
   (lambda (condition stream)
     (let ((*print-circle* t))
       (format stream "MOST-POSITIVE-FIXNUM or more units can flow on graph ~W."
               (max-flow-overflow-graph condition))))))

(defstruct (edge (:constructor %make-edge))
  (to nil :type (integer 0 #.most-positive-fixnum))
  (capacity 0 :type (integer 0 #.most-positive-fixnum))
  (reversed nil :type (or null edge)))

(defun push-edge (from-idx to-idx capacity graph &optional bidirectional)
  "FROM-IDX, TO-IDX := index of vertex
GRAPH := vector of lists

If BIDIRECTIONAL is true, PUSH-EDGE adds the reversed edge of the same capacity
in addition."
  (declare (optimize (speed 3))
           ((integer 0 #.most-positive-fixnum) from-idx to-idx)
           ((simple-array list (*)) graph))
  (let* ((dep (%make-edge :to to-idx :capacity capacity))
         (ret (%make-edge :to from-idx
                          :capacity (if bidirectional capacity 0)
                          :reversed dep)))
    (setf (edge-reversed dep) ret)
    (push dep (aref graph from-idx))
    (push ret (aref graph to-idx))))

(defun %fill-dist-table (src graph dist-table queue)
  "Does BFS and sets the distance between SRC and each vertex of GRAPH to
DIST-TABLE, where an edge of zero capacity is regarded as disconnected."
  (declare (optimize (speed 3) (safety 0))
           ((integer 0 #.most-positive-fixnum) src)
           ((simple-array list (*)) graph)
           ((simple-array (unsigned-byte 32) (*)) dist-table queue))
  (let* ((q-front 0)
         (q-end 0))
    (declare ((integer 0 #.most-positive-fixnum) q-front q-end))
    (labels ((enqueue (obj)
               (setf (aref queue q-end) obj)
               (incf q-end))
             (dequeue ()
               (prog1 (aref queue q-front)
                 (incf q-front))))
      (declare (inline enqueue dequeue))
      (fill dist-table +graph-max-distance+)
      (setf (aref dist-table src) 0)
      (enqueue src)
      (loop until (= q-front q-end)
            for vertex = (dequeue)
            do (dolist (edge (aref graph vertex))
                 (let ((neighbor (edge-to edge)))
                   (when (and (> (edge-capacity edge) 0)
                              (= +graph-max-distance+ (aref dist-table neighbor)))
                     (setf (aref dist-table neighbor)
                           (+ 1 (aref dist-table vertex)))
                     (enqueue neighbor)))))))
  dist-table)

(declaim (ftype (function * (values (integer 0 #.most-positive-fixnum) &optional)) %find-path))
(defun %find-path (src dest tmp-graph dist-table)
  "Finds an augmenting path, sends the maximal flow through it, and returns the
amount of the flow."
  (declare (optimize (speed 3) (safety 0))
           ((integer 0 #.most-positive-fixnum) src dest)
           ((simple-array list (*)) tmp-graph)
           ((simple-array (unsigned-byte 32) (*)) dist-table))
  (labels ((dfs (v flow)
             (declare ((integer 0 #.most-positive-fixnum) v flow))
             (when (= v dest)
               (return-from dfs flow))
             (loop
               (unless (aref tmp-graph v)
                 (return 0))
               (let ((edge (car (aref tmp-graph v))))
                 (when (and (> (edge-capacity edge) 0)
                            (< (aref dist-table v) (aref dist-table (edge-to edge))))
                   (let ((result (dfs (edge-to edge) (min flow (edge-capacity edge)))))
                     (declare ((integer 0 #.most-positive-fixnum) result))
                     (when (> result 0)
                       (decf (edge-capacity edge) result)
                       (incf (edge-capacity (edge-reversed edge)) result)
                       (return result)))))
               (setf (aref tmp-graph v)
                     (cdr (aref tmp-graph v))))))
    (dfs src most-positive-fixnum)))

(defun max-flow! (src dest graph)
  "Destructively sends the maximal flow from SRC to DEST and returns the amount
of the flow. This function signals MAX-FLOW-OVERFLOW error when an infinite
flow (to be precise, >= MOST-POSITIVE-FIXNUM) is possible."
  (declare ((integer 0 #.most-positive-fixnum) src dest)
           ((simple-array list (*)) graph))
  (let* ((n (length graph))
         (dist-table (make-array n :element-type '(unsigned-byte 32)))
         (queue (make-array n :element-type '(unsigned-byte 32)))
         (tmp-graph (make-array n :element-type 'list))
         (result 0))
    (declare ((integer 0 #.most-positive-fixnum) result))
    (loop
      (%fill-dist-table src graph dist-table queue)
      (when (= (aref dist-table dest) +graph-max-distance+) ; not (or no longer) connected
        (return result))
      (dotimes (i n)
        (setf (aref tmp-graph i) (aref graph i)))
      (loop for delta = (%find-path src dest tmp-graph dist-table)
            until (zerop delta)
            do (when (>= (+ result delta) most-positive-fixnum)
                 (error 'max-flow-overflow :graph graph))
               (incf result delta)))))

(declaim (inline read-line-into))
(defun read-line-into (buffer-string &key (in *standard-input*) (terminate-char #\Space))
  "Receives ASCII inputs and returns multiple values: the string and the end
position.

This function calls READ-BYTE to read characters though it calls READ-CHAR
instead on SLIME because SLIME's IO is not bivalent."
  (declare (inline read-byte)
           #-swank (sb-kernel:ansi-stream in))
  (loop for c of-type base-char =
           #-swank (code-char (read-byte in nil #.(char-code #\Newline)))
           #+swank (read-char in nil #\Newline)
        for idx from 0
        until (char= c #\Newline)
        do (setf (char buffer-string idx) c)
        finally (when (< idx (length buffer-string))
                  (setf (char buffer-string idx) terminate-char))
                (return (values buffer-string idx))))

(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+ most-positive-fixnum)

(defun main ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (src (* 2 h w))
         (dest (+ 1 (* 2 h w)))
         (graph (make-array (+ 2 (* 2 h w)) :element-type 'list :initial-element nil))
         (line (make-string w :element-type 'base-char)))
    (declare (uint8 h w))
    (labels ((calc (i j dir)
               (declare (uint8 i j))
               (+ (if (eql dir :in) 0 1) (* 2 (+ j (* i w))))))
      (dotimes (i h)
        (read-line-into line)
        (dotimes (j w)
          (when (or (= i 0) (= i (- h 1)) (= j 0) (= j (- w 1)))
            (push-edge (calc i j :out) dest +inf+ graph))
          (if (char= #\X (aref line j))
              (progn (push-edge src (calc i j :in) +inf+ graph)
                     (push-edge (calc i j :in) (calc i j :out) +inf+ graph))
              (push-edge (calc i j :in) (calc i j :out) 1 graph))
          (when (< 0 i)
            (push-edge (calc i j :out) (calc (- i 1) j :in) +inf+ graph))
          (when (< i (- h 1))
            (push-edge (calc i j :out) (calc (+ i 1) j :in) +inf+ graph))
          (when (< 0 j)
            (push-edge (calc i j :out) (calc i (- j 1) :in) +inf+ graph))
          (when (< j (- w 1))
            (push-edge (calc i j :out) (calc i (+ j 1) :in) +inf+ graph))))
      (handler-bind ((max-flow-overflow (lambda (c) (println -1) (return-from main))))
        (println (max-flow! src dest graph))))))

#-swank(main)


Measured memory_bytes: 35560