#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9;
const long long LINF = (long long)1e18;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
const int N = 500;
void TimeExecution() {
  cout << "\n\n===>> Time Execution: " << clock() / (double)1000 << " sec(s).";
}
int n, m;
int a[N + 1][N + 1];
long long res = 0;
bool flag = 0;
bool check(int x, int y, int val) {
  if (val > a[x - 1][y] && val < a[x][y + 1] && val > a[x][y - 1] &&
      val < a[x + 1][y])
    return 1;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (m); j++) {
      cin >> a[i][j];
      if (a[i][j] == 0) flag = 1;
    }
  if (flag == 0)
    for (int i = (1); i <= (n); i++)
      for (int j = (1); j <= (m); j++) {
        bool f1 = 1, f2 = 1;
        if (a[i][j] <= a[i][j - 1] && j > 1) f1 = 0;
        if (a[i][j] <= a[i - 1][j] && i > 1) f2 = 0;
        if (!f1 || !f2) {
          cout << -1;
          return 0;
        }
      }
  for (int i = (n - 1); i >= (1); i--) {
    for (int j = (m - 1); j >= (1); j--) {
      if (a[i][j] == 0) {
        bool pick = 0;
        for (int k = (a[i][j + 1]); k >= (1); k--) {
          if (check(i, j, k)) {
            a[i][j] = k;
            pick = 1;
            break;
          }
        }
        if (!pick) {
          cout << -1;
          return 0;
        }
      }
    }
  }
  for (int i = (1); i <= (n); i++) {
    for (int j = (1); j <= (m); j++) {
      res += a[i][j];
    }
  }
  cout << res;
  return 0;
}
