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