Merge grid cells when necessary