#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <vector>

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int32_t n, m;
    std::cin >> n >> m;
    std::vector<std::vector<int32_t>> g(n, std::vector<int32_t>(m));

    for (auto i = 0; i < n; ++i) {
        for (auto j = 0; j < m; ++j) {
            std::cin >> g[i][j];
        }
    }

    int32_t ans = 1e9;
    for (auto i1 = 0; i1 < n; ++i1) {
    for (auto j1 = 0; j1 < m; ++j1) {
        int32_t cur = 0;
        for (auto i2 = 0; i2 < n; ++i2) {
        for (auto j2 = 0; j2 < m; ++j2) {
            auto dist = std::abs(i1 - i2) + std::abs(j1 - j2);
            cur += g[i2][j2] * dist;
        }
        }

        ans = std::min(ans, cur);
    }
    }

    std::cout << ans << '\n';

    return 0;
}
