h, w = gets.chomp.split.map(&:to_i)
a = []
h.times{a << gets.chomp.split.map(&:to_i)}
min = nil
for y in 0...h
    for x in 0...w
        dist = 0
        for y2 in 0...h
            for x2 in 0...w
                dist += [(y2-y).abs, (x2-x).abs].min * a[y2][x2]
            end
        end
        min = dist if min.nil? || min > dist
    end
end
puts min
