Looking for ways to optimize this algorythm!

This algorythm is supposed to print all the islands and the coordinates of points that belong to the former. These islands belong to a 2D grid with 0s and 1s. 1s represent a block of the island and the 0s represent sea. Because the grid is somewhat large, my code is crashing when I try to run it. Ild appreciate help to try and optimize it!

function map.GetIslands(list)
	local copy = list
	local visited = {}
	local islands = {}
	local function dfs(x, y)
		local maxx, maxy = #list, #list[1]  
		if table.find(visited, {x, y}) == nil then
			table.insert(visited, {x, y})
		end
		if x > maxx or x < 1 or y > maxy or y < 1 or table.find(visited, {x, y}) ~= nil or list[x][y] == 0 then
			return 0
		end
		table.insert(islands[#islands], {x, y})
		return (1 + dfs(x + 1, y) + dfs(x - 1, y) + dfs(x, y + 1) + dfs(x, y - 1))
	end
	
	for x, yey in pairs(copy) do
		for y, val in pairs(yey) do
			if val == 1 and table.find(visited, {x, y}) == nil then
				task.wait()
				table.insert(islands, {})
				dfs(x, y)
			end
		end
	end
	
	print(islands)
end

If there is something you do not understand please let me know.
Edit: Note that I am talking a grid of about 75x125

not sure why it would error on you, though I do notice you have a lot of recursive functioning going on

I rewrote your function without recurcive functions, I included a simple map generator for my own testing


function module.arrayAppendArray(t, toAppend)
	for _,v in ipairs(toAppend) do
		t[#t+1] = v
	end
	return t
end

function module.tableDeepCopy(orig, copies) -- brought to you by http://lua-users.org/wiki/CopyTable
	copies = copies or {}
	local orig_type = type(orig)
	local copy
	if orig_type == 'table' then
		if copies[orig] then
			copy = copies[orig]
		else
			copy = {}
			copies[orig] = copy
			for orig_key, orig_value in next, orig, nil do
				copy[module.tableDeepCopy(orig_key, copies)] = module.tableDeepCopy(orig_value, copies)
			end
			setmetatable(copy, module.tableDeepCopy(getmetatable(orig), copies))
		end
	else -- number, string, boolean, etc
		copy = orig
	end
	return copy
end
local Mainframe = module



local map = {}
map.cords = {}
for x=1,50 do
	map.cords[x] = {}
	for y=1,50 do
		map.cords[x][y] = math.round(math.random(0,75)/100)
	end
end

function map.GetIslands(list :{{number}})
	list = map.cords --TODO: REMOVE THIS LINE WHEN YOU OVER WRITE YOUR FUNC
	type island = {Vector2}
	list = Mainframe.tableDeepCopy(list) ::{{number}}
	local mapSize = Vector2.new(#list, #list[1])
	local islands :{[Vector2] :island} = {}

	local function findAndMergeIsland(cord :Vector2, island :island)
		for _,islcord in pairs(island) do
			if islcord == cord then return end --don't merge ourselves
		end
		for foundIslandNumb,foundIsland in pairs(islands) do
			for _,fiCord in pairs(foundIsland) do
				if fiCord == cord then
					Mainframe.arrayAppendArray(island,foundIsland)
					islands[foundIslandNumb] = nil
					return
				end
			end
		end
		error("Didn't find an island even though we marked the position off")
	end

	local function startIsland(x :number, y :number)
		local island :island = {Vector2.new(x,y)}
		list[x][y]=nil

		for i=y-1,1,-1 do --up
			local nextpos = list[x][i]
			if nextpos then --we have not marked off this spot yet
				if nextpos==0 then break end --stop going because no longer island
				table.insert(island,Vector2.new(x,i))
				list[x][i] = nil --markoff so that we know we already searched there
			else findAndMergeIsland(Vector2.new(x,i),island) --we marked it off so we need to merge the islands
			end
		end
		for i=y+1,mapSize.Y do --down
			local nextpos = list[x][i]
			if nextpos then
				if nextpos==0 then break end
				table.insert(island,Vector2.new(x,i))
				list[x][i] = nil
			else findAndMergeIsland(Vector2.new(x,i),island)
			end
		end
		for i=x-1,1,-1 do --left
			local nextpos = list[i][y]
			if nextpos then
				if nextpos==0 then break end
				table.insert(island,Vector2.new(i,y))
				list[i][y] = nil
			else findAndMergeIsland(Vector2.new(i,y),island)
			end
		end
		for i=x+1,mapSize.X do --right
			local nextpos = list[i][y]
			if nextpos then
				if nextpos==0 then break end
				table.insert(island,Vector2.new(i,y))
				list[i][y] = nil
			else findAndMergeIsland(Vector2.new(i,y),island)
			end
		end
		
		
		islands[island[1]] = island
		return island
	end

	--once we mark off a cord in startIsland
	--it will skip over that cord since it doesn't exist
	--see http://lua-users.org/lists/lua-l/2012-07/msg00538.html
	for x, temp in pairs(list) do
		for y, isIsland in pairs(temp) do
			if isIsland == 1 then
				startIsland(x,y)
			end
		end
	end
	
	print(islands,list)
	return islands
end

map.GetIslands(nil)