#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, sum = 0;
  cin >> n >> m;
  int a[n][m];
  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], a[i][j + 1]) - 1;
      if (i != 0 && j != 0 &&
          (a[i][j] <= a[i - 1][j] || a[i][j] <= a[i][j - 1])) {
        cout << -1;
        return 0;
      }
      sum += a[i][j];
    }
  }
  cout << sum;
}
