Script crashes Blender when executed

I have this script

import bpy  
import time
import csv
 
A = time.time()
 
 
dfile = r"C:\Users\dealga\Desktop\Archive\SU8606_DSM_1M.asc"
 
getval = lambda i: int(next(i).split()[1])
 
with open(dfile) as ofile:
    ncols = getval(ofile)
    nrows = getval(ofile)
    xllcorner = getval(ofile)
    yllcorner = getval(ofile)
    cellsize = getval(ofile)
    NODATA_value = getval(ofile)
    
    print(ncols, nrows, xllcorner, yllcorner, cellsize, NODATA_value)
 
    # this will read the rest
    verts = []
    add_vert = verts.append
    asc_reader = csv.reader(ofile, delimiter=' ')
    
    # ni = nrows #? +1 -1
    # nj = ncols #? +1 -1
    ni = 1000
    nj = 1000
        
    for i, row in enumerate(asc_reader):
        if i >= ni:
            break
        for j in range(int(ncols)):
            if j >= nj:
                break
            z = (float(row[j]) / 60)
            x = j * 0.01   #   cell x width
            y = i * 0.01   #   cell y width
            add_vert((x,y,z))
            
    print('done')
    print('last vertex:', verts[-1])
 
B = time.time()
 
total_time = B-A
print('total_time:', total_time)
 
faces = []
add_face = faces.append
 
# generate_edges, i = verts y, j = verts x
total_range = ((ni-1) * (nj))
indices = []
for i in range(total_range):
    if not ((i+1) % nj == 0):
        add_face([i, i+nj, i+nj+1, i+1])
 
# do your own error handling
mesh_data = bpy.data.meshes.new("LIDAR_mesh_data3")  
mesh_data.from_pydata(verts, [], faces)  
mesh_data.update()
  
LIDAR_object = bpy.data.objects.new("LIDAR_Object3", mesh_data)  
  
scene = bpy.context.scene    
scene.objects.link(LIDAR_object)    
LIDAR_object.select = True  

which should import an ESRI ASCII DEM into Blender, but everytime I try to execute it Blender closes.
Note that I didn’t write the script, but I found it on the net and tried to understand it, so if there’s something deprecated or not in use anymore can you point it to me?
Thanks in advance

maybe mesh_data.validate() before mesh_data.update() helps.

I’ll try it later when I’ll return to home and I’ll let you know if it works

That doesn’t fix the problem. Maybe because the file is too big?

Are verts and faces compatible? I have seen Mesh.from_pydata crash Blender if e.g. the faces refer to a vertex number > len(verts).