#include <bits/stdc++.h>
using namespace std;
int num[500][500], type[500][500] = {0};
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int main() {
  int n, m, x, y, result, lift, right, sign;
  cin >> n >> m;
  result = 0;
  sign = 0;
  x = 0;
  memset(type, 0, sizeof(int));
  memset(num, 0, sizeof(int));
  while (x < n) {
    y = 0;
    while (y < m) {
      cin >> num[x][y];
      if (num[x][y] == 0) {
        type[x][y] = 1;
        lift = right = 0;
        if (y != 0) {
          if (num[x][y - 1] != 0 && num[x][y] == 0) {
            right = num[x][y - 1] + 1;
          }
        }
        if (x != 0) {
          if (num[x - 1][y] != 0 && num[x][y] == 0) {
            lift = num[x - 1][y] + 1;
          }
        }
        num[x][y] = max(lift, right);
      }
      result += num[x][y];
      if (type[x][y] != 1) {
        if (x != 0 && y != 0) {
          if (num[x - 1][y] >= num[x][y] || num[x][y - 1] >= num[x][y]) {
            sign = 1;
          }
        }
      }
      y++;
    }
    x++;
  }
  if (sign == 1) goto end;
  int minn, maxx;
  for (int i = n - 2; i >= 1; i--) {
    for (int ii = m - 2; ii >= 1; ii--) {
      if (type[i][ii] == 1) {
        if (i != 0) {
          if (ii != 0) {
            maxx = min(num[i][ii + 1], num[i + 1][ii]) - 1;
            minn = max(num[i - 1][ii], num[i][ii - 1]) + 1;
          }
        }
        if (maxx > minn) {
          result += (maxx - num[i][ii]);
          num[i][ii] = maxx;
        }
      }
    }
  }
end:
  if (sign == 1)
    cout << -1 << endl;
  else
    cout << result << endl;
  return 0;
}
