#include <bits/stdc++.h>
using namespace std;
const int M = 1000000007, BIG = 0x3f3f3f3f;
int n, m;
vector<vector<int> > a;
long long Process() {
  long long 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][j + 1] - 1, a[i + 1][j] - 1);
      if (j < m - 1 && a[i][j] >= a[i][j + 1]) return -1;
      if (i < n - 1 && a[i][j] >= a[i + 1][j]) return -1;
      ans += a[i][j];
    }
  return ans;
}
void Output() { cout << Process() << "\n"; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  while (cin >> n) {
    cin >> m;
    a.assign(n, vector<int>(m));
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) cin >> a[i][j];
    Output();
  }
  return 0;
}
