import Control.Applicative ((<$>))
import Control.Monad (replicateM)

main :: IO ()
main = do
  [h, w] <- map read . words <$> getLine
  ass <- replicateM h (map read . words <$> getLine)
  print $ solve h w ass
  
solve :: Int -> Int -> [[Int]] -> Int
solve h w ass = minimum [ sum [ (dist i j k l) * (ass !! (k - 1)) !! (l - 1) | k <- [1 .. h], l <- [1 .. w] ] | i <- [1 .. h], j <- [1 .. w] ]
  where
    dist i j k l = (abs (i - k)) `min` (abs (j - l)) 
