import           Control.Applicative
import           Data.Array
import           Data.List

main = do
  [h, w] <- fmap read . words <$> getLine :: IO [Int]
  dist <- (fmap (read :: String -> Int) . concatMap words . lines) <$> getContents :: IO [Int]
  let a = listArray ((1, 1), (h, w)) dist
  print $ minCost a

cost (i, j) = sum . fmap (\((i', j'), p) -> min (abs $ i - i') (abs $ j - j') * p) . assocs
minCost a = minimum $ fmap (`cost` a) $ indices a

