#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
long long T, n, m, k, x, y, z, l, r, q, ans, b[N], vis[N], mod = 1e9 + 7;
long long a[505][505];
void solve() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) cin >> a[i][j];
  bool ok = 1;
  for (int i = n; i; i--)
    for (int j = m; j; j--) {
      if (a[i][j] == 0) a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
      if ((i < n && a[i][j] >= a[i + 1][j]) ||
          (j < m && a[i][j] >= a[i][j + 1]))
        ok = 0;
      ans += a[i][j];
    }
  cout << (ok ? ans : -1);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
