#include <bits/stdc++.h>
using namespace std;
int ma[6000][6000];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      scanf("%d", &ma[i][j]);
      ma[n][j] = 8001;
    }
    ma[i][m] = 8001;
  }
  int sum = 0;
  for (int i = n - 1; i >= 0; --i) {
    for (int j = m - 1; j >= 0; --j) {
      if (ma[i][j] == 0) ma[i][j] = min(ma[i + 1][j], ma[i][j + 1]) - 1;
      if (ma[i][j] >= min(ma[i + 1][j], ma[i][j + 1])) {
        puts("-1");
        return 0;
      }
      sum += ma[i][j];
    }
  }
  printf("%d\n", sum);
  return 0;
}
