#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int matrix[600][600];
  int sumMatrix = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> matrix[i][j];
      sumMatrix += matrix[i][j];
    }
  }
  int i = n;
  while (i >= 1) {
    for (int j = m; j >= 1; j--) {
      if (matrix[i][j] == 0) {
        matrix[i][j] = min(matrix[i + 1][j], matrix[i][j + 1]) - 1;
        sumMatrix += matrix[i][j];
      }
    }
    i--;
  }
  for (i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (matrix[i][j] <= matrix[i - 1][j] ||
          matrix[i][j] <= matrix[i][j - 1]) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << sumMatrix << endl;
  return 0;
}
