That's not your about your segmentation fault problem, but you should really consider to use a single malloc call to allocate all necessary memory space need by grid.
grid = malloc (nrows * ncols * sizeof(int *))
Look at Bill ONeary answer regarding pointer of pointer of pointer.