#include <bits/stdc++.h>
using namespace std;
int n, m, a[503][503], b[503][503];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
  int cnt = 0;
  for (int i = n - 1; i > 0; i--)
    for (int j = m - 1; j > 0; j--)
      if (a[i][j])
        b[i][j] = a[i][j];
      else
        b[i][j] = min(b[i + 1][j], b[i][j + 1]) - 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (!a[i][j]) a[i][j] = b[i][j];
      cnt += a[i][j];
    }
  }
  for (int i = 1; i < n; i++)
    for (int j = 0; j < m; j++)
      if (a[i][j] <= a[i - 1][j]) {
        cout << -1;
        return 0;
      }
  for (int i = 0; i < n; i++)
    for (int j = 1; j < m; j++)
      if (a[i][j] <= a[i][j - 1]) {
        cout << -1;
        return 0;
      }
  cout << cnt;
}
