import java.util.* ;
interface rest_tgr {
   static long best(int[] a, long v, long tp) {
      long r = v ;
      long right = tp ;
      long left = 0 ;
      for (int i=1; i<a.length; i++) {
         right -= a[i-1] ;
         left += a[i-1] ;
         v += left-right ;
         r = Math.min(r, v) ;
      }
      return r ;
   }
   static void main(String[] args) {
      Scanner sc = new Scanner(System.in) ;
      int r = sc.nextInt() ;
      int c = sc.nextInt() ;
      int sumr[] = new int[c] ;
      int sumc[] = new int[r] ;
      long src=0, scc=0, tp=0 ;
      for (int i=0; i<r; i++) {
         for (int j=0; j<c; j++) {
            int p = sc.nextInt() ;
            sumc[i] += p ;
            sumr[j] += p ;
            src += i * p ;
            scc += j * p ;
            tp += p ;
         }
      }
      System.out.println(best(sumr, scc, tp) + best(sumc, src, tp)) ;
   }
}
