#include <bits/stdc++.h>
using namespace std;
int a[505][505];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  memset(a, 0x3f3f3f3f, sizeof(a));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]);
  for (int i = n; i >= 1; i--)
    for (int j = m; j >= 1; j--) {
      if (a[i][j] == 0)
        a[i][j] = min(a[i][j + 1], a[i + 1][j]) - 1;
      else {
        if (a[i][j] >= a[i + 1][j] || a[i][j] >= a[i][j + 1]) {
          cout << "-1" << endl;
          return 0;
        }
      }
    }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) ans += a[i][j];
  printf("%d\n", ans);
  return 0;
}
