### Godot version

4.1 Stable 

### System information

Windows 11, Intel(R) Core(TM) i5-10400F CPU, NVIDIA GeForce GTX 1060 6GB, 24 ram

### Issue description

When deleting Terrains in the tileset resource, on terrain sets, then going on terrains on the tilemap editor, on the tilemap tab, it crashes for some reason, it even happens eventually if you delete the Tile images under the TileSet Tab before you delete the terrains under terrain sets.

### Steps to reproduce

1. Drag and drop my tilemap packed scene onto the overworld scene.
2. Make the packed scene local, then make the tileset resource unique.
3. Delete those Terrains in the tileset resource, on terrain sets.
4. Go on terrains on the tilemap editor, on the tilemap tab. If it doesnt crash at first, switch back to the tileset tab and then go back to the tilemap tab and in the terrains section.

### Minimal reproduction project

[BreezeVillage.zip](https://github.com/godotengine/godot/files/12069715/BreezeVillage.zip)

ERR_FAIL_COND_V(p_terrain_set < 0 || p_terrain_set >= get_terrain_sets_count(), Vector<Point2>());
	TileSet::TerrainMode terrain_mode = get_terrain_set_mode(p_terrain_set);
	if (tile_shape == TileSet::TILE_SHAPE_SQUARE) {
		if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
			return _get_square_corner_or_side_terrain_peering_bit_polygon(tile_size, p_bit);
		} else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
			return _get_square_corner_terrain_peering_bit_polygon(tile_size, p_bit);
		} else { 
			return _get_square_side_terrain_peering_bit_polygon(tile_size, p_bit);
		}
	} else if (tile_shape == TileSet::TILE_SHAPE_ISOMETRIC) {
		if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS_AND_SIDES) {
			return _get_isometric_corner_or_side_terrain_peering_bit_polygon(tile_size, p_bit);
		} else if (terrain_mode == TileSet::TERRAIN_MODE_MATCH_CORNERS) {
			return _get_isometric_corner_terrain_peering_bit_polygon(tile_size, p_bit);
		} else { 
			return _get_isometric_side_terrain_peering_bit_polygon(tile_size, p_bit);
		}
	} else {
		float overlap = 0.0;
		switch (tile_shape) {
			case TileSet::TILE_SHAPE_HEXAGON: