#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  bool possible = true;
  cin >> n >> m;
  int matrix[n][m];
  int sumMatrix = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> matrix[i][j];
      sumMatrix += matrix[i][j];
    }
  }
  int i = n - 1;
  while (possible && i >= 0) {
    for (int j = m - 1; j >= 0; j--) {
      if (matrix[i][j] == 0) {
        matrix[i][j] = min(matrix[i + 1][j], matrix[i][j + 1]) - 1;
        sumMatrix += matrix[i][j];
      }
    }
    i--;
  }
  i = 0;
  while (possible && i < n) {
    for (int j = 0; j < m; j++) {
      if (i > 0 && j > 0) {
        if (matrix[i][j] <= max(matrix[i - 1][j], matrix[i][j - 1])) {
          possible = false;
          break;
        }
      } else if (i == 0 && j != 0) {
        if (matrix[i][j] <= matrix[i][j - 1]) {
          possible = false;
          break;
        }
      } else if (j == 0 && i != 0) {
        if (matrix[i][j] <= matrix[i - 1][j]) {
          possible = false;
          break;
        }
      }
    }
    i++;
  }
  if (possible) {
    cout << sumMatrix << endl;
  } else {
    cout << -1 << endl;
  }
}
