#include <bits/stdc++.h>
using namespace std;
long long a[600][600];
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i][j];
  for (int i = n - 2; i >= 0; i--)
    if (!a[i][m - 1]) a[i][m - 1] = a[i + 1][m - 1] - 1;
  for (int i = m - 2; i >= 0; i--)
    if (!a[n - 1][i]) a[n - 1][i] = a[n - 1][i + 1] - 1;
  for (int i = n - 2; i >= 0; i--)
    for (int j = m - 2; j >= 0; j--)
      if (!a[i][j]) a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
  for (int i = 1; i < n; i++)
    if (a[i][0] <= a[i - 1][0]) return puts("-1"), 0;
  for (int i = 1; i < m; i++)
    if (a[0][i] <= a[0][i - 1]) return puts("-1"), 0;
  for (int i = 1; i < n; i++)
    for (int j = 1; j < m; j++)
      if (a[i][j] <= a[i - 1][j] || a[i][j] <= a[i][j - 1])
        return puts("-1"), 0;
  long long sum = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) sum += a[i][j];
  cout << sum << endl;
  return 0;
}
