#line 1 "atcoder-workspace/acl1/c/c.cpp"
#line 1 "atcoder-workspace/acl1/c/c.cpp"
#include <bits/extc++.h>

#line 7 "Library/alias.hpp"
namespace workspace {
constexpr char eol = '\n';
using namespace std;
using i32 = int_least32_t;
using i64 = int_least64_t;
using i128 = __int128_t;
using u32 = uint_least32_t;
using u64 = uint_least64_t;
using u128 = __uint128_t;
template <class T, class Comp = less<T>>
using priority_queue = std::priority_queue<T, vector<T>, Comp>;
template <class T> using stack = std::stack<T, vector<T>>;
constexpr i32 clz32(const u32 &n) noexcept { return __builtin_clz(n); }
constexpr i32 clz64(const u64 &n) noexcept { return __builtin_clzll(n); }
constexpr i32 ctz(const u64 &n) noexcept { return __builtin_ctzll(n); }
constexpr i32 popcnt(const u64 &n) noexcept { return __builtin_popcountll(n); }
}  // namespace workspace
#line 5 "Library/config.hpp"
namespace config {
const auto start_time{std::chrono::system_clock::now()};
int64_t elapsed() {
  using namespace std::chrono;
  const auto end_time{system_clock::now()};
  return duration_cast<milliseconds>(end_time - start_time).count();
}
__attribute__((constructor)) void setup() {
  using namespace std;
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
#ifdef _buffer_check
  atexit([] {
    char bufc;
    if (cin >> bufc)
      cerr << "\n\033[43m\033[30mwarning: buffer not empty.\033[0m\n\n";
  });
#endif
}
unsigned cases(void), caseid = 1;
template <class C> void main() {
  for (const unsigned total = cases(); caseid <= total; ++caseid) C();
}
}  // namespace config
#line 2 "Library/option.hpp"
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2")
#pragma GCC optimize("unroll-loops")
#endif
#line 5 "Library/utils/binary_search.hpp"
namespace workspace {
// binary search on discrete range.
template <class iter_type, class pred_type>
std::enable_if_t<
    std::is_convertible_v<std::invoke_result_t<pred_type, iter_type>, bool>,
    iter_type>
binary_search(iter_type ok, iter_type ng, pred_type pred) {
  assert(ok != ng);
  __int128_t dist(ng - ok);
  while (dist > 1 || dist < -1) {
    iter_type mid(ok + dist / 2);
    if (pred(mid))
      ok = mid, dist -= dist / 2;
    else
      ng = mid, dist /= 2;
  }
  return ok;
}
// parallel binary search on discrete range.
template <class iter_type, class pred_type>
std::enable_if_t<std::is_convertible_v<
                     std::invoke_result_t<pred_type, std::vector<iter_type>>,
                     std::vector<bool>>,
                 std::vector<iter_type>>
binary_search(std::vector<std::pair<iter_type, iter_type>> ends,
              pred_type pred) {
  std::vector<iter_type> mids(ends.size());
  for (;;) {
    bool all_found = true;
    for (size_t i{}; i != ends.size(); ++i) {
      auto [ok, ng] = ends[i];
      iter_type mid(ok + (ng - ok) / 2);
      if (mids[i] != mid) {
        all_found = false;
        mids[i] = mid;
      }
    }
    if (all_found) break;
    auto res = pred(mids);
    for (size_t i{}; i != ends.size(); ++i) {
      (res[i] ? ends[i].first : ends[i].second) = mids[i];
    }
  }
  return mids;
}
// binary search on real numbers.
template <class real_type, class pred_type>
std::enable_if_t<
    std::is_convertible_v<std::invoke_result_t<pred_type, real_type>, bool>,
    real_type>
binary_search(real_type ok, real_type ng, const real_type eps, pred_type pred) {
  assert(ok != ng);
  while (ok + eps < ng || ng + eps < ok) {
    real_type mid{(ok + ng) / 2};
    (pred(mid) ? ok : ng) = mid;
  }
  return ok;
}
// parallel binary search on real numbers.
template <class real_type, class pred_type>
std::enable_if_t<std::is_convertible_v<
                     std::invoke_result_t<pred_type, std::vector<real_type>>,
                     std::vector<bool>>,
                 std::vector<real_type>>
binary_search(std::vector<std::pair<real_type, real_type>> ends,
              const real_type eps, pred_type pred) {
  std::vector<real_type> mids(ends.size());
  for (;;) {
    bool all_found = true;
    for (size_t i{}; i != ends.size(); ++i) {
      auto [ok, ng] = ends[i];
      if (ok + eps < ng || ng + eps < ok) {
        all_found = false;
        mids[i] = (ok + ng) / 2;
      }
    }
    if (all_found) break;
    auto res = pred(mids);
    for (size_t i{}; i != ends.size(); ++i) {
      (res[i] ? ends[i].first : ends[i].second) = mids[i];
    }
  }
  return mids;
}
}  // namespace workspace
#line 3 "Library/utils/casefmt.hpp"
namespace workspace {
std::ostream &casefmt(std::ostream &os) {
  return os << "Case #" << config::caseid << ": ";
}
}  // namespace workspace
#line 3 "Library/utils/chval.hpp"
namespace workspace {
template <class T, class Comp = std::less<T>>
bool chle(T &x, const T &y, Comp comp = Comp()) {
  return comp(y, x) ? x = y, true : false;
}
template <class T, class Comp = std::less<T>>
bool chge(T &x, const T &y, Comp comp = Comp()) {
  return comp(x, y) ? x = y, true : false;
}
}  // namespace workspace
#line 4 "Library/utils/coordinate_compression.hpp"

template <class T> class coordinate_compression {
  std::vector<T> uniquely;
  std::vector<size_t> compressed;

 public:
  coordinate_compression(const std::vector<T> &raw)
      : uniquely(raw), compressed(raw.size()) {
    std::sort(uniquely.begin(), uniquely.end());
    uniquely.erase(std::unique(uniquely.begin(), uniquely.end()),
                   uniquely.end());
    for (size_t i = 0; i != size(); ++i)
      compressed[i] =
          std::lower_bound(uniquely.begin(), uniquely.end(), raw[i]) -
          uniquely.begin();
  }

  size_t operator[](const size_t idx) const {
    assert(idx < size());
    return compressed[idx];
  }

  size_t size() const { return compressed.size(); }

  size_t count() const { return uniquely.size(); }

  T value_of(const size_t ord) const {
    assert(ord < count());
    return uniquely[ord];
  }

  size_t order_of(const T &val) const {
    return std::lower_bound(uniquely.begin(), uniquely.end(), val) -
           uniquely.begin();
  }

  std::vector<size_t>::iterator begin() { return compressed.begin(); }
  std::vector<size_t>::iterator end() { return compressed.end(); }
  std::vector<size_t>::reverse_iterator rbegin() { return compressed.rbegin(); }
  std::vector<size_t>::reverse_iterator rend() { return compressed.rend(); }
};
#line 3 "Library/utils/fixed_point.hpp"
namespace workspace {
// specify the return type of lambda.
template <class lambda_type> class fixed_point {
  lambda_type func;

 public:
  fixed_point(lambda_type &&f) : func(std::move(f)) {}
  template <class... Args> auto operator()(Args &&... args) const {
    return func(*this, std::forward<Args>(args)...);
  }
};
}  // namespace workspace
#line 6 "Library/utils/hash.hpp"

#line 3 "Library/utils/sfinae.hpp"
#include <type_traits>

template <class type, template <class> class trait>
using enable_if_trait_type = typename std::enable_if<trait<type>::value>::type;

template <class Container>
using element_type = typename std::decay<decltype(
    *std::begin(std::declval<Container &>()))>::type;

template <class T, class = int> struct mapped_of {
  using type = element_type<T>;
};
template <class T>
struct mapped_of<T,
                 typename std::pair<int, typename T::mapped_type>::first_type> {
  using type = typename T::mapped_type;
};
template <class T> using mapped_type = typename mapped_of<T>::type;

template <class T, class = void> struct is_integral_ext : std::false_type {};
template <class T>
struct is_integral_ext<
    T, typename std::enable_if<std::is_integral<T>::value>::type>
    : std::true_type {};
template <> struct is_integral_ext<__int128_t> : std::true_type {};
template <> struct is_integral_ext<__uint128_t> : std::true_type {};
#if __cplusplus >= 201402
template <class T>
constexpr static bool is_integral_ext_v = is_integral_ext<T>::value;
#endif

template <typename T, typename = void> struct multiplicable_uint {
  using type = uint_least32_t;
};
template <typename T>
struct multiplicable_uint<T, typename std::enable_if<(2 < sizeof(T))>::type> {
  using type = uint_least64_t;
};
template <typename T>
struct multiplicable_uint<T, typename std::enable_if<(4 < sizeof(T))>::type> {
  using type = __uint128_t;
};
#line 8 "Library/utils/hash.hpp"
namespace workspace {
template <class T, class = void> struct hash : std::hash<T> {};
template <class Unique_bits_type>
struct hash<Unique_bits_type,
            enable_if_trait_type<Unique_bits_type,
                                 std::has_unique_object_representations>> {
  size_t operator()(uint64_t x) const {
    static const uint64_t m = std::random_device{}();
    x ^= x >> 23;
    x ^= m;
    x ^= x >> 47;
    return x - (x >> 32);
  }
};
template <class Key> size_t hash_combine(const size_t &seed, const Key &key) {
  return seed ^
         (hash<Key>()(key) + 0x9e3779b9 /* + (seed << 6) + (seed >> 2) */);
}
template <class T1, class T2> struct hash<std::pair<T1, T2>> {
  size_t operator()(const std::pair<T1, T2> &pair) const {
    return hash_combine(hash<T1>()(pair.first), pair.second);
  }
};
template <class... T> class hash<std::tuple<T...>> {
  template <class Tuple, size_t index = std::tuple_size<Tuple>::value - 1>
  struct tuple_hash {
    static uint64_t apply(const Tuple &t) {
      return hash_combine(tuple_hash<Tuple, index - 1>::apply(t),
                          std::get<index>(t));
    }
  };
  template <class Tuple> struct tuple_hash<Tuple, size_t(-1)> {
    static uint64_t apply(const Tuple &t) { return 0; }
  };

 public:
  uint64_t operator()(const std::tuple<T...> &t) const {
    return tuple_hash<std::tuple<T...>>::apply(t);
  }
};
template <class hash_table> struct hash_table_wrapper : hash_table {
  using key_type = typename hash_table::key_type;
  size_t count(const key_type &key) const {
    return hash_table::find(key) != hash_table::end();
  }
  template <class... Args> auto emplace(Args &&... args) {
    return hash_table::insert(typename hash_table::value_type(args...));
  }
};
template <class Key, class Mapped = __gnu_pbds::null_type>
using cc_hash_table =
    hash_table_wrapper<__gnu_pbds::cc_hash_table<Key, Mapped, hash<Key>>>;
template <class Key, class Mapped = __gnu_pbds::null_type>
using gp_hash_table =
    hash_table_wrapper<__gnu_pbds::gp_hash_table<Key, Mapped, hash<Key>>>;
template <class Key, class Mapped>
using unordered_map = std::unordered_map<Key, Mapped, hash<Key>>;
template <class Key> using unordered_set = std::unordered_set<Key, hash<Key>>;
}  // namespace workspace
#line 3 "Library/utils/make_vector.hpp"
namespace workspace {
template <typename T, size_t N>
constexpr auto make_vector(size_t *sizes, T const &init = T()) {
  if constexpr (N)
    return std::vector(*sizes, make_vector<T, N - 1>(std::next(sizes), init));
  else
    return init;
}
template <typename T, size_t N>
constexpr auto make_vector(const size_t (&sizes)[N], T const &init = T()) {
  return make_vector<T, N>((size_t *)sizes, init);
}
}  // namespace workspace
#line 3 "Library/utils/random_number_generator.hpp"
template <typename num_t> class random_number_generator {
  template <bool is_int, class = void> struct unif_t {
    std::uniform_int_distribution<num_t> unif;
    unif_t(num_t lower, num_t upper) : unif(lower, upper) {}
    num_t operator()(std::mt19937 &engine) { return unif(engine); }
  };
  template <class void_t> struct unif_t<false, void_t> {
    std::uniform_real_distribution<num_t> unif;
    unif_t(num_t lower, num_t upper) : unif(lower, upper) {}
    num_t operator()(std::mt19937 &engine) { return unif(engine); }
  };
  unif_t<std::is_integral<num_t>::value> unif;
  std::mt19937 engine;

 public:
  // generate random number in [lower, upper].
  random_number_generator(num_t lower = std::numeric_limits<num_t>::min(),
                          num_t upper = std::numeric_limits<num_t>::max())
      : unif(lower, upper), engine(std::random_device{}()) {}
  num_t operator()() { return unif(engine); }
};  // class random_number_generator
#line 3 "Library/utils/read.hpp"
namespace workspace {
// read with std::cin.
template <class T = void> struct read {
  typename std::remove_const<T>::type value;
  template <class... types> read(types... args) : value(args...) {
    std::cin >> value;
  }
  operator T() const { return value; }
};
template <> struct read<void> {
  template <class T> operator T() const {
    T value;
    std::cin >> value;
    return value;
  }
};
}  // namespace workspace
#line 4 "Library/utils/stream.hpp"

#line 6 "Library/utils/stream.hpp"
namespace std {
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ' ' << p.second;
}
template <class tuple_t, size_t index> struct tuple_is {
  static istream &apply(istream &is, tuple_t &t) {
    tuple_is<tuple_t, index - 1>::apply(is, t);
    return is >> get<index>(t);
  }
};
template <class tuple_t> struct tuple_is<tuple_t, SIZE_MAX> {
  static istream &apply(istream &is, tuple_t &t) { return is; }
};
template <class... T> istream &operator>>(istream &is, tuple<T...> &t) {
  return tuple_is<tuple<T...>, tuple_size<tuple<T...>>::value - 1>::apply(is,
                                                                          t);
}
template <class tuple_t, size_t index> struct tuple_os {
  static ostream &apply(ostream &os, const tuple_t &t) {
    tuple_os<tuple_t, index - 1>::apply(os, t);
    return os << ' ' << get<index>(t);
  }
};
template <class tuple_t> struct tuple_os<tuple_t, 0> {
  static ostream &apply(ostream &os, const tuple_t &t) {
    return os << get<0>(t);
  }
};
template <class tuple_t> struct tuple_os<tuple_t, SIZE_MAX> {
  static ostream &apply(ostream &os, const tuple_t &t) { return os; }
};
template <class... T> ostream &operator<<(ostream &os, const tuple<T...> &t) {
  return tuple_os<tuple<T...>, tuple_size<tuple<T...>>::value - 1>::apply(os,
                                                                          t);
}
template <class Container, typename Value = element_type<Container>>
typename enable_if<!is_same<typename decay<Container>::type, string>::value &&
                       !is_same<typename decay<Container>::type, char *>::value,
                   istream &>::type
operator>>(istream &is, Container &cont) {
  for (auto &&e : cont) is >> e;
  return is;
}
template <class Container, typename Value = element_type<Container>>
typename enable_if<!is_same<typename decay<Container>::type, string>::value &&
                       !is_same<typename decay<Container>::type, char *>::value,
                   ostream &>::type
operator<<(ostream &os, const Container &cont) {
  bool head = true;
  for (auto &&e : cont) head ? head = 0 : (os << ' ', 0), os << e;
  return os;
}
}  // namespace std
#line 4 "Library/utils/trinary_search.hpp"
// trinary search on discrete range.
template <class iter_type, class comp_type>
iter_type trinary(iter_type first, iter_type last, comp_type comp) {
  assert(first < last);
  intmax_t dist(last - first);
  while (dist > 2) {
    iter_type left(first + dist / 3), right(first + dist * 2 / 3);
    if (comp(left, right))
      last = right, dist = dist * 2 / 3;
    else
      first = left, dist -= dist / 3;
  }
  if (dist > 1 && comp(first + 1, first)) ++first;
  return first;
}
// trinary search on real numbers.
template <class comp_type>
long double trinary(long double first, long double last, const long double eps,
                    comp_type comp) {
  assert(first < last);
  while (last - first > eps) {
    long double left{(first * 2 + last) / 3}, right{(first + last * 2) / 3};
    if (comp(left, right))
      last = right;
    else
      first = left;
  }
  return first;
}
#line 2 "Library/utils/wrapper.hpp"
template <class Container> class reversed {
  Container &ref, copy;

 public:
  reversed(Container &ref) : ref(ref) {}
  reversed(Container &&ref = Container()) : ref(copy), copy(ref) {}
  auto begin() const { return ref.rbegin(); }
  auto end() const { return ref.rend(); }
};
#line 7 "atcoder-workspace/acl1/c/c.cpp"
namespace workspace {
struct solver;
}  // namespace workspace
int main() { config::main<workspace::solver>(); }
unsigned config::cases() {
  // return -1; // unspecified
  // int t; std::cin >> t; return t; // given
  return 1;
}

#line 4 "Library/graph/directed/flow/base.hpp"
// the base class of flow algorithms.
template <class cap_t, class cost_t> struct flow_base {
  struct edge_t {
    size_t src, dst;
    cap_t cap;
    cost_t cost;
    edge_t *rev;
    edge_t() = default;
    edge_t(size_t src, size_t dst, const cap_t &cap, edge_t *rev)
        : src(src), dst(dst), cap(cap), rev(rev) {}
    edge_t(size_t src, size_t dst, const cap_t &cap, const cost_t &cost,
           edge_t *rev)
        : src(src), dst(dst), cap(cap), cost(cost), rev(rev) {}
    const cap_t &flow(const cap_t &f = 0) { return cap -= f, rev->cap += f; }
    bool avbl() const { return static_cast<cap_t>(0) < cap; }
  };  // class edge_t

  class adj_type {
    edge_t *fst, *lst, *clst;

   public:
    template <class... Args> edge_t *emplace(Args &&... args) {
      if (lst == clst) {
        size_t len(clst - fst);
        edge_t *nfst = lst = new edge_t[len << 1];
        for (edge_t *p{fst}; p != clst; ++p, ++lst)
          p->rev->rev = lst, *lst = *p;
        delete[] fst;
        fst = nfst;
        clst = lst + len;
      }
      *lst = edge_t(args...);
      return lst++;
    }
    adj_type() : fst(new edge_t[1]), lst(fst), clst(fst + 1) {}
    ~adj_type() { delete[] fst; }
    edge_t &operator[](size_t i) {
      assert(i < size());
      return *(fst + i);
    }
    size_t size() const { return lst - fst; }
    edge_t *begin() const { return fst; }
    edge_t *end() const { return lst; }
  };  // class adj_type

  flow_base(size_t n = 0) : adjs(n) {}

  flow_base(const flow_base &other) : adjs(other.size()) {
    for (size_t node{}; node != size(); ++node)
      for (const auto &[src, dst, cap, cost, rev] : other[node])
        if (src == node) {
          edge_t *ptr = adjs[src].emplace(src, dst, cap, cost, nullptr);
          ptr->rev = adjs[dst].emplace(dst, src, rev->cap, -cost, ptr);
          rev->src = nil;
        } else {
          rev->rev->src = node;
        }
  }

  flow_base &operator=(const flow_base &rhs) {
    if (this != &rhs) adjs.swap(flow_base(rhs).adjs);
    return *this;
  }

  size_t size() const { return adjs.size(); }

  adj_type &operator[](size_t node) {
    assert(node < size());
    return adjs[node];
  }
  const adj_type &operator[](size_t node) const {
    assert(node < size());
    return adjs[node];
  }

  virtual edge_t *add_edge(size_t src, size_t dst, const cap_t &cap,
                           const cost_t &cost) {
    assert(src < size());
    assert(dst < size());
    assert(!(cap < static_cast<cap_t>(0)));
    if (!(static_cast<cap_t>(0) < cap) || src == dst) return nullptr;
    edge_t *ptr = adjs[src].emplace(src, dst, cap, cost, nullptr);
    ptr->rev = adjs[dst].emplace(dst, src, 0, -cost, ptr);
    return ptr;
  }

 protected:
  constexpr static size_t nil = -1;
  std::vector<adj_type> adjs;
};  // class flow_base
#line 3 "Library/graph/directed/flow/Dinic.hpp"
// compute the maximum flow.
template <class cap_t = int> class Dinic : public flow_base<cap_t, bool> {
  using base = flow_base<cap_t, bool>;
  using edge_t = typename base::edge_t;
  using base::adjs;

  std::vector<size_t> level;
  std::vector<edge_t *> itr;
  constexpr static size_t level_infty = -1;

  cap_t dfs(const size_t &src, const size_t &dst, cap_t bound) {
    if (src == dst || bound == 0) return bound;
    cap_t flow(0);
    for (edge_t *&e{itr[dst]}; e != adjs[dst].end(); ++e)
      if (e->rev->avbl() && level[e->dst] < level[dst])
        if (cap_t achv = dfs(src, e->dst, std::min(bound, e->rev->cap));
            achv > 0) {
          e->rev->flow(achv);
          flow += achv, bound -= achv;
          if (bound == 0) break;
        }
    return flow;
  }

 public:
  using base::size;

  Dinic(size_t n = 0) : base::flow_base(n), level(n, level_infty), itr(n) {}

  Dinic(const Dinic &other)
      : base::flow_base(other), level(other.level), itr(other.itr) {}

  Dinic &operator=(const Dinic &rhs) {
    if (this != &rhs) base::operator=(rhs), level = rhs.level, itr = rhs.itr;
    return *this;
  }

  void add_edge(size_t src, size_t dst, cap_t cap) {
    base::add_edge(src, dst, cap, false);
  }

  void add_undirected_edge(size_t src, size_t dst, cap_t cap) {
    base::add_undirected_edge(src, dst, cap, false);
  }

  cap_t max_flow(size_t src, size_t dst) {
    assert(src < size());
    assert(dst < size());
    cap_t flow(0), bound(0);
    for (const edge_t &e : adjs[src]) bound += e.cap;
    for (std::vector<size_t> que(size());;
         std::fill(level.begin(), level.end(), level_infty)) {
      level[que.front() = src] = 0;
      for (auto ql{que.begin()}, qr{std::next(ql)};
           level[dst] == level_infty && ql != qr; ++ql)
        for (const edge_t &e : adjs[*ql])
          if (e.avbl() && level[e.dst] == level_infty)
            level[ *qr++ = e.dst] = level[*ql] + 1;
      if (level[dst] == level_infty) break;
      for (size_t node{}; node != size(); ++node)
        itr[node] = adjs[node].begin();
      flow += dfs(src, dst, bound);
    }
    return flow;
  }
};  // class Dinic
#line 4 "Library/graph/directed/flow/min_cost_flow.hpp"

#line 6 "Library/graph/directed/flow/min_cost_flow.hpp"
// Successive shortest paths algorithm.
template <class cap_t, class cost_t>
class min_cost_flow : public flow_base<cap_t, cost_t> {
  using base = flow_base<cap_t, cost_t>;
  using edge_t = typename base::edge_t;
  using base::adjs;
  using base::nil;

  cost_t min_cost, total_cost;
  std::vector<cap_t> supp;
  std::vector<cost_t> ptnl;

  void copy_member(const min_cost_flow &other) {
    min_cost = other.min_cost;
    total_cost = other.total_cost;
    supp = other.supp;
    ptnl = other.ptnl;
  }

  void Dijkstra(std::vector<edge_t *> &last) {
    const cost_t infty(total_cost + 1);
    std::vector<cost_t> nptnl(size(), infty);
    //*/ // O((V + E)logV)
    struct node_t {
      size_t id;
      cost_t dist;
      node_t(size_t id, cost_t dist) : id(id), dist(dist) {}
      bool operator<(const node_t &rhs) const { return rhs.dist < dist; }
    };
    std::priority_queue<node_t> que;
    for (size_t src{}; src != size(); ++src) {
      if (supp[src] > static_cast<cap_t>(0)) {
        nptnl[src] = 0;
        for (edge_t &e : adjs[src]) {
          if (supp[e.dst] > static_cast<cap_t>(0)) continue;
          if (e.avbl() && nptnl[e.dst] > e.cost) {
            que.emplace(e.dst, (nptnl[e.dst] = e.cost) - ptnl[e.dst]);
            last[e.dst] = &e;
          }
        }
      }
    }
    while (!que.empty()) {
      auto [src, ndist] = que.top();
      que.pop();
      if (ndist + ptnl[src] != nptnl[src]) continue;
      for (edge_t &e : adjs[src]) {
        if (e.avbl() && nptnl[e.dst] > nptnl[src] + e.cost) {
          que.emplace(e.dst,
                      (nptnl[e.dst] = nptnl[src] + e.cost) - ptnl[e.dst]);
          last[e.dst] = &e;
        }
      }
    }
    /*/ // O(V^2)
    std::vector<bool> used(size());
    for (size_t src{}; src != size(); ++src) {
      if (static_cast<cap_t>(0) < supp[src]) {
        used[src] = true;
        nptnl[src] = 0;
        for (edge_t &e : adjs[src]) {
          if (static_cast<cap_t>(0) < supp[e.dst]) continue;
          if (e.avbl() && e.cost < nptnl[e.dst]) {
            nptnl[e.dst] = e.cost;
            last[e.dst] = &e;
          }
        }
      }
    }
    for (;;) {
      size_t src{nil};
      cost_t sp{infty};
      for (size_t node{}; node != size(); ++node) {
        if (used[node] || nptnl[node] == infty) continue;
        cost_t dist{nptnl[node] - ptnl[node]};
        if (dist < sp) {
          sp = dist;
          src = node;
        }
      }
      if (src == nil) break;
      used[src] = true;
      for (edge_t &e : adjs[src]) {
        if (e.avbl() && nptnl[src] + e.cost < nptnl[e.dst]) {
          nptnl[e.dst] = nptnl[src] + e.cost;
          last[e.dst] = &e;
        }
      }
    }
    //*/
    ptnl.swap(nptnl);
  }

 public:
  using base::size;

  min_cost_flow(size_t n = 0)
      : base::flow_base(n), min_cost(0), total_cost(0), supp(n), ptnl(n) {}

  min_cost_flow(const min_cost_flow &other) : base::flow_base(other) {
    copy_member(other);
  }

  min_cost_flow &operator=(const min_cost_flow &other) {
    base::operator=(other);
    copy_member(other);
    return *this;
  }

  edge_t *add_edge(size_t src, size_t dst, const cost_t &cost);

  edge_t *add_edge(size_t src, size_t dst, const cap_t &cap,
                   const cost_t &cost) override {
    assert(src != dst);
    if (cost < static_cast<cost_t>(0)) {
      supp[src] -= cap;
      supp[dst] += cap;
      min_cost += cap * cost;
      total_cost -= cap * cost;
      return base::add_edge(dst, src, cap, -cost);
    }
    total_cost += cap * cost;
    return base::add_edge(src, dst, cap, cost);
  }

  edge_t *add_edge(size_t src, size_t dst, const cap_t &lower,
                   const cap_t &upper, const cost_t &cost) {
    assert(!(upper < lower));
    supp[src] -= lower;
    supp[dst] += lower;
    min_cost += lower * cost;
    return add_edge(src, dst, upper - lower, cost);
  }

  const cap_t &supply(size_t node, const cap_t &vol = 0) {
    assert(node < size());
    return supp[node] += vol;
  }

  const cap_t &demand(size_t node, const cap_t &vol) {
    return supply(node, -vol);
  }

  bool flow() {
    for (bool aug = true; aug;) {
      aug = false;
      std::vector<edge_t *> last(size());
      Dijkstra(last);
      std::vector<bool> shut(size());
      for (size_t dst{}; dst != size(); ++dst) {
        if (supp[dst] < static_cast<cap_t>(0) and last[dst]) {
          cap_t resid{-supp[dst]};
          size_t src{dst}, block{nil};
          while (last[src] && !shut[src]) {
            if (!(resid < last[src]->cap)) resid = last[block = src]->cap;
            src = last[src]->src;
          }
          if (shut[src])
            block = src;
          else {
            if (!(resid < supp[src])) {
              resid = supp[src];
              block = src;
            }
            for (edge_t *e{last[dst]}; e; e = last[e->src]) {
              e->cap -= resid;
              e->rev->cap += resid;
            }
            supp[src] -= resid;
            supp[dst] += resid;
            min_cost += ptnl[dst] * resid;
            aug = true;
          }
          if (~block) {
            for (size_t node{dst};; node = last[node]->src) {
              shut[node] = true;
              if (node == block) break;
            }
          }
        }
      }
    }
    return std::none_of(begin(supp), end(supp),
                        [](const cap_t &s) { return s < 0 || 0 < s; });
  }

  cost_t optimal() {
    assert(flow());
    return min_cost;
  }
};  // class min_cost_flow
#line 19 "atcoder-workspace/acl1/c/c.cpp"

struct workspace::solver {
  solver() {
    // start here!
    int n, m;
    cin >> n >> m;
    min_cost_flow<int, int> mcf(n * m + 2);
    int src = n * m;
    int dst = src + 1;
    const int inf = 100;
    auto vid = [&](int i, int j) { return i * m + j; };
    for (auto i = 0; i < n; ++i) {
      string s;
      cin >> s;
      for (int j = 0; j < m; j++) {
        if ('#' == s[j]) continue;
        if ('o' == s[j]) {
          mcf.supply(vid(i, j), 1);
          mcf.demand(dst, 1);
        }
        mcf.add_edge(vid(i, j), dst, 1, 0);
        if (i + 1 < n) mcf.add_edge(vid(i, j), vid(i + 1, j), inf, -1);
        if (j + 1 < m) mcf.add_edge(vid(i, j), vid(i, 1 + j), inf, -1);
      }
    }
    cout << -mcf.optimal() << eol;
  }
};


Measured memory_bytes: 4252