#include <bits/stdc++.h>
using namespace std;
int n, m;
int matrix[600][600];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i <= n + 1; i++)
    for (int j = 0; j <= m + 1; j++) matrix[i][j] = 10000;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &matrix[i][j]);
  long long int sum = 0;
  for (int i = n; i > 0; i--) {
    for (int j = m; j > 0; j--) {
      if (matrix[i][j] == 0) {
        matrix[i][j] = min(matrix[i + 1][j], matrix[i][j + 1]) - 1;
      }
      sum += matrix[i][j];
    }
  }
  int flag = true;
  for (int 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])
        flag = false;
    }
  }
  if (flag)
    printf("%lld\n", sum);
  else
    printf("-1\n");
}
