import java.util.*


data class Coordinate(val x:Int, val y:Int) {
    operator fun plus(that:Coordinate): Coordinate = Coordinate(x + that.x, y + that.y)
}
val Coordinate.neighbors: Iterable<Coordinate>
    get() {
        return listOf(Coordinate(0, 1), Coordinate(0, -1), Coordinate(1, 0), Coordinate(-1, 0)).map{this + it}
    }
fun main(args:Array<String>){
    val (h, w) = readLine()!!.trim().split(' ').map(String::toInt)
    val forest = Array(h){readLine()!!.trim().split(' ').map(String::toInt).toIntArray()}
    val queue = PriorityQueue<Triple<Int, Int, Coordinate>>(compareBy(Triple<Int, Int, Coordinate>::first)).apply{add(Triple(0, 0, Coordinate(0, 0)))}
    val memo = Array(h){Array(w){IntArray(h * w){Int.MAX_VALUE}}}.apply{this[0][0][0] = 0}
    while (queue.peek().third != Coordinate(h - 1, w - 1)){
        val (time, distance, coordinate) = queue.poll()
        if (distance < h * w - 1 && memo[coordinate.x][coordinate.y][distance] == time){
            for ((x, y) in coordinate.neighbors){
                if (x in 0 until h && y in 0 until w && memo[x][y][distance + 1] > time + (2 * distance + 1) * forest[x][y]){
                    memo[x][y][distance + 1] = time + (2 * distance + 1) * forest[x][y]
                    queue.add(Triple(memo[x][y][distance + 1], distance + 1, Coordinate(x, y)))
                }
            }
        }
    }
    println(queue.peek().first)
}
