#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  int a[n + 5][m + 5];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) cin >> a[i][j];
  for (int i = 1; i <= n; ++i) a[i][m + 1] = 1e9;
  for (int j = 1; j <= m; ++j) a[n + 1][j] = 1e9;
  for (int i = n; i >= 1; --i)
    for (int j = m; j >= 1; --j)
      if (a[i][j] == 0) a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
  bool ok = true;
  int sum = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      sum += a[i][j];
      ok &= (a[i][j] < a[i + 1][j]) && (a[i][j] < a[i][j + 1]);
    }
  cout << ((ok) ? sum : -1);
  return 0;
}
