h,w=gets.split(" ").map{|e| e.to_i} a=[[0]*(w+1)] b=[[0]*(w+1)] h.times{|y| a<<gets.split(" ").map{|e| e.to_i} a[y+1].unshift(0) b[y+1]=[0]*(w+1) } s1=[0]*(w+1) 1.upto(h){|y| s0=0 1.upto(w){|x| s1[x]+=a[y][x]*y*x s0=s0+s1[x]+a[y][x]*x*y b[y][x]=s1[x]+s0 } } p b ans=0 h.times{|y1| w.times{|x1| y1.upto(h-1){|y2| x1.upto(w-1){|x2| y1.upto(y2){|y3| x1.upto(x2){|x3| ans+=a[y3+1][x3+1] } } } } } } puts ans
4 5 3 5 7 2 5 1 2 2 3 1 4 8 4 8 4 3 9 1 1 5
[[0, 0, 0, 0, 0, 0], [0, 9, 36, 89, 92, 159], [0, 12, 51, 111, 166, 214], [0, 46, 209, 317, 600, 722], [0, 70, 389, 425, 648, 994]] 2784