#include <iostream>
#include <vector>

using namespace std;

int main() {
  int r, c;
  cin >> r >> c;
  vector<vector<int>> g(r);
  for(auto& x: g) {
    x.resize(c);
    for(auto& y: x) cin >> y;
  }
  int ret = 1e9;
  for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) {
    int cand = 0;
    for(int a = 0; a < r; a++) for(int b = 0; b < c; b++) {
      cand += (abs(i-a)+abs(j-b))*g[a][b];
    }
    ret = min(ret, cand);
  }
  cout << ret << "\n";
}