import java.util.* ;
interface restsimp_tgr {
   static void main(String[] args) {
      Scanner sc = new Scanner(System.in) ;
      int r = sc.nextInt() ;
      int c = sc.nextInt() ;
      int pp[][] = new int[r][c] ; ;
      for (int i=0; i<r; i++) {
         for (int j=0; j<c; j++) {
            int p = sc.nextInt() ;
            pp[i][j] = p ;
         }
      }
      long best = 1000000000000000000L ;
      for (int i=0; i<r; i++) {
         for (int j=0; j<c; j++) {
            long v = 0 ;
            for (int ii=0; ii<r; ii++)
               for (int jj=0; jj<c; jj++)
                  v += (Math.abs(ii-i)+Math.abs(jj-j)) * pp[ii][jj] ;
            best = Math.min(v, best) ;
         }
      }
      System.out.println(best) ;
   }
}
