#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  cout.tie(0);
  int n, m, a[501][501], totale = 0;
  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--)
    for (int j = m - 2; j > 0; j--) {
      if (a[i][j] == 0) {
        if (max(a[i - 1][j], a[i][j - 1]) + 1 < min(a[i + 1][j], a[i][j + 1])) {
          a[i][j] = min(a[i + 1][j], a[i][j + 1]) - 1;
        } else {
          cout << -1;
          return 0;
        }
      }
    }
  for (int i = 0; i < n - 1; i++)
    if (a[i][m - 1] < a[i + 1][m - 1])
      totale += a[i][m - 1];
    else {
      cout << -1;
      return 0;
    }
  for (int i = 0; i < m - 1; i++)
    if (a[n - 1][i] < a[n - 1][i + 1])
      totale += a[n - 1][i];
    else {
      cout << -1;
      return 0;
    }
  for (int i = 0; i < n - 1; i++)
    for (int j = 0; j < m - 1; j++)
      if ((a[i][j]) < min(a[i + 1][j], a[i][j + 1]))
        totale += a[i][j];
      else {
        cout << -1;
        return 0;
      }
  cout << totale + a[n - 1][m - 1];
  return 0;
}
