#include <bits/stdc++.h>
const long long int INF = 1e18 + 5;
const int MaxN = 505;
using namespace std;
long long int t, n, m, k, a[MaxN][MaxN], ans = 0;
string s;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  int ok = 1;
  ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = m - 1; j >= 0; j--) {
      if (a[i][j] == 0) {
        a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
      } else {
        if ((i < n - 1 && a[i][j] >= a[i + 1][j]) ||
            (j < m - 1 && a[i][j] >= a[i][j + 1])) {
          ok = 0;
          break;
        }
      }
      ans += a[i][j];
    }
    if (!ok) break;
  }
  if (ok)
    cout << ans << "\n";
  else
    cout << -1 << "\n";
  return 0;
}
