#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d, e, f = 0, g = 0, h, i, p;
  scanf("%d%d", &a, &b);
  int z[a + 10][b + 10];
  for (i = 0; i < a; i++)
    for (p = 0; p < b; p++) scanf("%d", &z[i][p]);
  for (i = a - 2; i >= 0; i--)
    for (p = b - 2; p >= 0; p--)
      if (z[i][p] == 0) {
        c = z[i + 1][p] > z[i][p + 1] ? z[i][p + 1] : z[i + 1][p];
        z[i][p] = c - 1;
      }
  for (i = 0; i < a; i++)
    for (p = 0; p < b - 1; p++)
      if (z[i][p] >= z[i][p + 1]) f = 1;
  if (f == 0)
    for (i = 0; i < b; i++)
      for (p = 0; p < a - 1; p++)
        if (z[p][i] >= z[p + 1][i]) f = 1;
  if (f == 0) {
    for (i = 0; i < a; i++)
      for (p = 0; p < b; p++) g += z[i][p];
    printf("%d\n", g);
  } else
    printf("-1\n");
}
