#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
int arr[n + 2][m + 2];
for (int i = 0; i < m + 2; i++)
{
arr[0][i] = 0;
arr[n + 1][i] = 0;
}
for (int i = 0; i < n + 2; i++)
{
arr[i][0] = 0;
arr[i][m + 1] = 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> arr[i][j];
int ans = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
int curr1 = 0, curr2 = 0, curr3 = 0, curr4 = 0;
for (int k = i + 1; k < n + 2; k++)
curr1 = max(curr1, arr[k][j]);
for (int k = 0; k < i; k++)
curr2 = max(curr2, arr[k][j]);
for (int k = 0; k < j; k++)
curr3 = max(curr3, arr[i][k]);
for (int k = j + 1; k < m + 2; k++)
curr4 = max(curr4, arr[i][k]);
int ans_curr = min(curr1, curr2);
ans_curr = min(ans_curr, curr3);
ans_curr = min(ans_curr, curr4);
if (ans_curr > arr[i][j])
ans += ans_curr - arr[i][j];
}
}
cout << ans << '\n';
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgaW50IGxvbmcgbG9uZwoKc2lnbmVkIG1haW4oKQp7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7CgogICAgaW50IG4sIG07CiAgICBjaW4gPj4gbiA+PiBtOwogICAgaW50IGFycltuICsgMl1bbSArIDJdOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtICsgMjsgaSsrKQogICAgewogICAgICAgIGFyclswXVtpXSA9IDA7CiAgICAgICAgYXJyW24gKyAxXVtpXSA9IDA7CiAgICB9CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG4gKyAyOyBpKyspCiAgICB7CiAgICAgICAgYXJyW2ldWzBdID0gMDsKICAgICAgICBhcnJbaV1bbSArIDFdID0gMDsKICAgIH0KCiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspCiAgICAgICAgZm9yIChpbnQgaiA9IDE7IGogPD0gbTsgaisrKQogICAgICAgICAgICBjaW4gPj4gYXJyW2ldW2pdOwoKICAgIGludCBhbnMgPSAwOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKQogICAgewogICAgICAgIGZvciAoaW50IGogPSAxOyBqIDw9IG07IGorKykKICAgICAgICB7CiAgICAgICAgICAgIGludCBjdXJyMSA9IDAsIGN1cnIyID0gMCwgY3VycjMgPSAwLCBjdXJyNCA9IDA7CiAgICAgICAgICAgIGZvciAoaW50IGsgPSBpICsgMTsgayA8IG4gKyAyOyBrKyspCiAgICAgICAgICAgICAgICBjdXJyMSA9IG1heChjdXJyMSwgYXJyW2tdW2pdKTsKCiAgICAgICAgICAgIGZvciAoaW50IGsgPSAwOyBrIDwgaTsgaysrKQogICAgICAgICAgICAgICAgY3VycjIgPSBtYXgoY3VycjIsIGFycltrXVtqXSk7CgogICAgICAgICAgICBmb3IgKGludCBrID0gMDsgayA8IGo7IGsrKykKICAgICAgICAgICAgICAgIGN1cnIzID0gbWF4KGN1cnIzLCBhcnJbaV1ba10pOwoKICAgICAgICAgICAgZm9yIChpbnQgayA9IGogKyAxOyBrIDwgbSArIDI7IGsrKykKICAgICAgICAgICAgICAgIGN1cnI0ID0gbWF4KGN1cnI0LCBhcnJbaV1ba10pOwoKICAgICAgICAgICAgaW50IGFuc19jdXJyID0gbWluKGN1cnIxLCBjdXJyMik7CiAgICAgICAgICAgIGFuc19jdXJyID0gbWluKGFuc19jdXJyLCBjdXJyMyk7CiAgICAgICAgICAgIGFuc19jdXJyID0gbWluKGFuc19jdXJyLCBjdXJyNCk7CgogICAgICAgICAgICBpZiAoYW5zX2N1cnIgPiBhcnJbaV1bal0pCiAgICAgICAgICAgICAgICBhbnMgKz0gYW5zX2N1cnIgLSBhcnJbaV1bal07CiAgICAgICAgfQogICAgfQogICAgY291dCA8PCBhbnMgPDwgJ1xuJzsKfQ==