#include <bits/stdc++.h>
using namespace std;
int n, m;
int A[500][500];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = (0); i < (n); ++i)
    for (int j = (0); j < (m); ++j) {
      cin >> A[i][j];
    }
  for (int i = n - 2; i >= 0; i--) {
    for (int j = m - 2; j >= 0; j--) {
      if (A[i][j] == 0)
        A[i][j] = min(A[i + 1][j], A[i][j + 1]) - 1;
      else if (A[i][j] >= min(A[i + 1][j], A[i][j + 1])) {
        cout << -1 << "\n";
        return 0;
      }
    }
  }
  if (A[n - 1][m - 1] <= max(A[n - 2][m - 1], A[n - 1][m - 2])) {
    cout << -1 << "\n";
  } else {
    int s = 0;
    for (int i = (0); i < (n); ++i)
      for (int j = (0); j < (m); ++j) s += A[i][j];
    cout << s << "\n";
  }
}
