### 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_INDEX(p_terrain_set, terrain_sets.size());
	ERR_FAIL_INDEX(p_terrain_index, terrain_sets[p_terrain_set].terrains.size());
	terrain_sets.write[p_terrain_set].terrains.write[p_terrain_index].name = p_name;
	emit_changed();
}
String TileSet::get_terrain_name(int p_terrain_set, int p_terrain_index) const {
	ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), String());
	ERR_FAIL_INDEX_V(p_terrain_index, terrain_sets[p_terrain_set].terrains.size(), String());
	return terrain_sets[p_terrain_set].terrains[p_terrain_index].name;
}
void TileSet::set_terrain_color(int p_terrain_set, int p_terrain_index, Color p_color) {
	ERR_FAIL_INDEX(p_terrain_set, terrain_sets.size());
	ERR_FAIL_INDEX(p_terrain_index, terrain_sets[p_terrain_set].terrains.size());
	if (p_color.a != 1.0) {
		WARN_PRINT("Terrain color should have alpha == 1.0");
		p_color.a = 1.0;
	}
	terrain_sets.write[p_terrain_set].terrains.write[p_terrain_index].color = p_color;
	emit_changed();
}
Color TileSet::get_terrain_color(int p_terrain_set, int p_terrain_index) const {
	ERR_FAIL_INDEX_V(p_terrain_set, terrain_sets.size(), Color());
	ERR_FAIL_INDEX_V(p_terrain_index, terrain_sets[p_terrain_set].terrains.size(), Color());
	return terrain_sets[p_terrain_set].terrains[p_terrain_index].color;
}
bool TileSet::is_valid_terrain_peering_bit_for_mode(TileSet::TerrainMode p_terrain_mode, TileSet::CellNeighbor p_peering_bit) const {