#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int X, Y;
  while (cin >> Y >> X) {
    vector<vector<int>> G(Y, vector<int>(X));
    for (auto& v : G) for (auto& x : v) cin >> x;

    int ret = 1e9;
    for (int y = 0; y < Y; y++) for (int x = 0; x < X; x++) {
      int cur = 0;
      for (int y2 = 0; y2 < Y; y2++) for (int x2 = 0; x2 < X; x2++) {
        cur += G[y2][x2] * (abs(y-y2) + abs(x-x2));
      }
      ret = min(ret, cur);
    }
    cout << ret << endl;
  }
}
