#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, ans = 0, r = 1;
  cin >> n >> m;
  long long int a[n][m], i, j;
  for (i = 0; i < n; ++i)
    for (j = 0; j < m; ++j) cin >> a[i][j];
  for (i = n - 1; i >= 0; --i)
    for (j = m - 1; j >= 0; --j) {
      if (a[i][j] == 0) a[i][j] = min(a[i + 1][j] - 1, a[i][j + 1] - 1);
      ans = ans + a[i][j];
    }
  for (i = 0; i < n; ++i)
    for (j = 0; j < m - 1; ++j)
      if (a[i][j] >= a[i][j + 1]) r = 0;
  for (j = 0; j < m; ++j)
    for (i = 0; i < n - 1; ++i)
      if (a[i][j] >= a[i + 1][j]) r = 0;
  if (r == 0)
    cout << "-1";
  else
    cout << ans;
  return 0;
}
