
object Main extends App {

  import scala.io.StdIn._
  import scala.math.{abs, min}
  val Array(h, w) = readLine.trim.split(' ').map(_.toInt)
  val population: Array[Array[Int]] = Array.tabulate(h){ _ ⇒ readLine.trim.split(' ').map(_.toInt)}
  println(
    (0 until h).flatMap{y ⇒ (0 until w).map{x ⇒ sumDistance(y, x, population)}}.min
  )
  def sumDistance(h: Int, w: Int, population: Array[Array[Int]]): Int = {
    population.indices.map{y ⇒
      population(y).indices.map{x ⇒
        population(y)(x) * min(abs(h - y), abs(w - x))
      }.sum
    }.sum
  }
}

