local ior = io.input() local h, w = ior:read("*n", "*n", "*l") local function getindex(i_h, i_w) return i_w + (i_h - 1) * w end local bsize = 2 local mapbox = {} for i = 1, math.ceil(h * w / bsize) do mapbox[i] = 0 end local function setval(idx, val) local boxidx = math.ceil(idx / bsize) local boxval = mapbox[boxidx] local newval, mul = 0, 1 local boxpos = idx % bsize for i = 0, bsize - 1 do if(i == boxpos) then newval = newval + mul * (val % 2) else newval = newval + mul * (boxval % 2) end boxval = math.floor(boxval / 2) mul = mul * 2 end mapbox[boxidx] = newval end local function getval(idx) local boxidx = math.ceil(idx / bsize) local boxval = mapbox[boxidx] local boxpos = idx % bsize for i = 1, boxpos do boxval = math.floor(boxval / 2) end return boxval % 2 end local str = "" for i_h = 1, h do str = ior:read() for i_w = 1, w do local idx = getindex(i_h, i_w) if(str:sub(i_w * 2 - 1, i_w * 2 - 1) == "1") then setval(idx, 1) else setval(idx, 0) end end end local tasks = {} local tasknum = 0 local done = 0 local tasklim = h + w local function addtask(idx) setval(idx, 0) tasknum = tasknum + 1 local taskidx = tasknum % tasklim if taskidx == 0 then taskidx = tasklim end tasks[taskidx] = idx end local curpos = 1 local lakecount = 0 local function searchtask() for i = curpos, h * w do if(getval(i) == 1) then lakecount = lakecount + 1 curpos = i addtask(i) break end end end searchtask() while(done < tasknum) do done = done + 1 local taskidx = done % tasklim if(taskidx == 0) then taskidx = tasklim end local idx = tasks[taskidx] if(w < idx) then if(getval(idx - w) == 1) then addtask(idx - w) end end if(idx <= (h - 1) * w) then if(getval(idx + w) == 1) then addtask(idx + w) end end if(1 < w) then if(idx % w ~= 0) then if(getval(idx + 1) == 1) then addtask(idx + 1) end end if(idx % w ~= 1) then if(getval(idx - 1) == 1) then addtask(idx - 1) end end end if(done == tasknum) then searchtask() end end print(lakecount)