#!/usr/bin/python3

g = []

r, c = map(int,input().split())

for i in range(r):
	temp_list =  list(map(int,input().split()))
	g.append(temp_list)

ans = 1000000000
for i in range(r):
	for j in range(c):
		loc = 0
		for x in range(r):
			for y in range(c):
				loc += (abs(i-x) + abs(j-y)) * g[x][y]
		ans = min(ans, loc)

print(ans)
