import scala.collection.mutable.ArrayBuffer

object Main extends App {
  import scala.io.StdIn._

  solve
  def solve: Unit = {
    val Array(s, d) = readLine.trim.split(' ').map(_.toInt)
    if (s == 0 && d == 0) return
    val union = Array.tabulate(s + d){_ ⇒ new UnionFind}
    for (i ← 1 until s){
      union(i).merge(union(i - 1))
    }
    val edges = ArrayBuffer[Edge]()
    for (f ← 0 until s){
      for ((distance, t) ← readLine.trim.split(' ').map(_.toInt).zipWithIndex.map{case (a, b) ⇒ (a, b + s)} if distance != 0){
        edges.append(Edge(f, t, distance))
      }
    }
    for (f ← 0 until d - 1){
      for ((distance, t) ← readLine.trim.split(' ').map(_.toInt).zipWithIndex.map{case (a, b) ⇒ (a, b + s + f + 1)} if distance != 0){
        edges.append(Edge(f + s, t, distance))
      }
    }
    var cost = 0
    for (e ← edges.sortBy(_.cost) if !union(e.to).isSameUnion(union(e.from))) {
      cost += e.cost
      union(e.to).merge(union(e.from))
    }
    println(cost)
    solve
  }
  case class Edge(from: Int, to: Int, cost: Int)
  class UnionFind {
    private var mParent = this
    private var mSize = 1
    private def parent(): UnionFind = {
      if (mParent != this){
        mParent = mParent.parent()
      }
      mParent
    }
    def isSameUnion(that: UnionFind): Boolean = parent() == that.parent()
    def merge(that: UnionFind): Unit = {
      if (!isSameUnion(that)){
        val thisP = this.parent()
        val thatP = that.parent()
        if (thisP.mSize < thatP.mSize){
          thisP.mParent = thatP
          thatP.mSize += thisP.mSize
        }else {
          thatP.mParent = thisP
          thisP.mSize += thatP.mSize
        }
      }
    }
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    @inline
    def let[A](func: T ⇒ A): A = func(value)
    @inline
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
  }
}
