;; -*- 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
;;;
;;; Ford-Fulkerson
;;;

(setf *print-circle* t)

(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 &key bidirectional)
  "FROM-IDX, TO-IDX := index of vertex
GRAPH := vector of lists of all the edges that goes from each vertex

If BIDIRECTIONAL is true, PUSH-EDGE adds the reversed edge of the same
capacity in addition."
  (declare (optimize (speed 3))
           ((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))))

(declaim (ftype (function * (values (integer 0 #.most-positive-fixnum) &optional)) %find-flow))
(defun %find-flow (src-idx dest-idx graph checked)
  "DFS"
  (declare (optimize (speed 3) (safety 0))
           ((integer 0 #.most-positive-fixnum) src-idx dest-idx)
           (simple-bit-vector checked)
           ((simple-array list (*)) graph))
  (fill checked 0)
  (labels ((dfs (vertex flow)
             (declare ((integer 0 #.most-positive-fixnum) flow))
             (setf (aref checked vertex) 1)
             (if (= vertex dest-idx)
                 flow
                 (dolist (edge (aref graph vertex) 0)
                   (when (and (zerop (aref checked (edge-to edge)))
                              (> (edge-capacity edge) 0))
                     (let ((flow (dfs (edge-to edge) (min flow (edge-capacity edge)))))
                       (declare ((integer 0 #.most-positive-fixnum) flow))
                       (unless (zerop flow)
                         (decf (edge-capacity edge) flow)
                         (incf (edge-capacity (edge-reversed edge)) flow)
                         (return flow))))))))
    (dfs src-idx most-positive-fixnum)))

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

(defun max-flow! (src-idx dest-idx graph)
  (declare (optimize (speed 3))
           ((integer 0 #.most-positive-fixnum) src-idx dest-idx)
           ((simple-array list (*)) graph))
  (let ((checked (make-array (length graph) :element-type 'bit :initial-element 0))
        (result 0))
    (declare ((integer 0 #.most-positive-fixnum) result))
    (loop
      (let ((increment (%find-flow src-idx dest-idx graph checked)))
        (cond ((zerop increment)
               (return result))
              ((>= (+ result increment) most-positive-fixnum)
               (error 'max-flow-overflow :graph graph))
              (t
               (incf result increment)))))))

(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: 39012