#include <bits/stdc++.h>
using namespace std;
const int64_t N = 1e3 + 5, M = 1e9 + 7;
int64_t a[N][N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int64_t n, m, s = 0;
  cin >> n >> m;
  for (int64_t i = 0; i < n; i++)
    for (int64_t j = 0; j < m; j++) cin >> a[i][j];
  for (int64_t i = n - 2; i; i--)
    for (int64_t j = m - 2; j; j--)
      if (!a[i][j]) a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
  for (int64_t i = 0; i < n; i++)
    for (int64_t j = 1; j < m; j++)
      if (a[i][j] <= a[i][j - 1]) return cout << -1, 0;
  for (int64_t j = 0; j < m; j++)
    for (int64_t i = 1; i < n; i++)
      if (a[i][j] <= a[i - 1][j]) return cout << -1, 0;
  for (int64_t i = 0; i < n; i++)
    for (int64_t j = 0; j < m; j++) s += a[i][j];
  cout << s;
}
