#include <bits/stdc++.h>
using namespace std;
int N, M, A[505][505], sum = 0;
template <class T>
inline void read(T &m) {
  m = 0;
  register char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) m = (m << 3) + (m << 1) + (c & 15), c = getchar();
}
int main() {
  read(N), read(M);
  for (register int i = 1; i <= N; ++i) {
    for (register int j = 1; j <= M; ++j) {
      read(A[i][j]);
    }
  }
  for (register int i = N; i; --i) {
    for (register int j = M; j; --j) {
      if (!A[i][j]) {
        A[i][j] = min(A[i + 1][j], A[i][j + 1]) - 1;
      }
      if (A[i - 1][j] >= A[i][j] || A[i][j - 1] >= A[i][j]) {
        puts("-1");
        return 0;
      }
      sum += A[i][j];
    }
  }
  printf("%d", sum);
}
