Go: package main

import (
	"fmt"
)

func main() {
	var h, w int
	for {
		fmt.Scan(&w, &h)
		if w == 0 && h == 0 {
			return
		}
		matrix := make([][]int, h)
		Sean := make([][]bool, h)
		for i := 0; i < h; i++ {
			matrix[i] = make([]int, w)
			Sean[i] = make([]bool, w)
			for j := 0; j < w; j++ {
				fmt.Scan(&matrix[i][j])
			}
		}
		var count int
		for i := 0; i < h; i++ {
			for j := 0; j < w; j++ {
				if matrix[i][j] == 1 && Sean[i][j] == false {
					search(i, j, h, w, matrix, Sean)
					count++
				}
			}
		}
		fmt.Println(count)
	}
}
func search(x int, y int, h int, w int, matrix [][]int, Sean [][]bool) {
	if x < 0 || y < 0 || x >= h || y >= w || matrix[x][y] == 0 {
		return
	}
	if Sean[x][y] == true {
		return
	}

	Sean[x][y] = true

	search(x+1, y, h, w, matrix, Sean)
	search(x-1, y, h, w, matrix, Sean)
	search(x, y+1, h, w, matrix, Sean)
	search(x, y-1, h, w, matrix, Sean)
	search(x+1, y-1, h, w, matrix, Sean)
	search(x-1, y+1, h, w, matrix, Sean)
	search(x+1, y+1, h, w, matrix, Sean)
	search(x-1, y-1, h, w, matrix, Sean)
}

Python: import sys


class UnionFind:
    def __init__(self, n=0):
        self.d = [-1]*n
        self.u = n

    def root(self, x):
        if self.d[x] < 0:
            return x
        self.d[x] = self.root(self.d[x])
        return self.d[x]

    def unite(self, x, y):
        x, y = self.root(x), self.root(y)
        if x == y:
            return False
        if x > y:
            x, y = y, x
        self.d[x] += self.d[y]
        self.d[y] = x
        self.u -= 1
        return True

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.d[self.root(x)]

    def num_union(self):
        return self.u


input = sys.stdin.readline
while True:
    W, H = map(int, input()[:-1].split())
    if W == 0:
        break
    S = [input()[:-1].split() for _ in [0]*H]

    uf = UnionFind(H*W)
    cnt = 0
    for h, ss in enumerate(S):
        for w, s in enumerate(ss):
            if s == '0':
                cnt += 1
                continue

            if h and w and S[h-1][w-1] == '1':
                uf.unite(h*W+w, (h-1)*W+w-1)
            if h and S[h-1][w] == '1':
                uf.unite(h*W+w, (h-1)*W+w)
            if h and w+1<W and S[h-1][w+1] == '1':
                uf.unite(h*W+w, (h-1)*W+w+1)
            if w and S[h][w-1] == '1':
                uf.unite(h*W+w, h*W+w-1)


    print(uf.u-cnt)

Python: import queue
def allvisited():
    for i in range(w):
        for j in range(h):
            if (visited[i][j]==False and s[i][j]=='1'):
                return (False,i,j)
    else:
         return (True,-1,-1)
        
def legal(pos):
    if(0<=pos[0]<w) and (0<=pos[1]<h) and s[pos[0]][pos[1]]=='1' and visited[pos[0]][pos[1]]==False:
        return True
    else:
        return False
    
d=[[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]
while(1):
    h,w=map(eval,input().split())
    if (w==0 and h==0): break
    s=[]
    for i in range(w):
        s.append(input().split())
    #print(s)
    Q=queue.Queue()
    visited=[]
    for i in range(w):
        visited.append([False]*h)
    #print(visited)
    count=0
    while(1):
        a=allvisited()
        if (a[0]==False):
            Q.put((a[1],a[2]))
            count+=1
        else:
            break
        while(not Q.empty()):
            now=Q.get()
            visited[now[0]][now[1]]=True
            for i in range(8):
                new=(now[0]+d[i][0],now[1]+d[i][1])
                if(legal(new)):
                    Q.put(new)
    print(count)

Python: import sys
readline = sys.stdin.buffer.readline

from collections import deque
import copy

def myinput():
    return map(int,readline().split())

def mycol(data,col):
    return [ row[col] for row in data ]

ls_ans = []

w,h = myinput()
c = [ list(myinput()) for _ in range(h) ]

while w!=0 or h!=0:
    def dfs(sx,sy):
        stack = deque()
        stack.append([sx,sy])
        while stack:
            x,y = stack.pop()
            if c[x][y]==1:
                c[x][y] = 0
                for i in range(-1,2):
                    for j in range(-1,2):
                        if i==0 and j==0:
                            continue
                        else:
                            nx = x + i
                            ny = y + j
                            if nx==-1 or ny==-1 or nx==h or ny==w:
                                pass
                            else:
                                if c[nx][ny]==1:
                                    stack.append([nx,ny])
                                else:
                                    pass
            else:
                pass

    ans = 0
    for i in range(h):
        for j in range(w):
            if c[i][j]==1:
                dfs(i,j)
                ans += 1
            else:
                pass
    # print(ans)
    ls_ans.append(ans)

    w,h = myinput()
    c = [ list(myinput()) for _ in range(h) ]

for i in range(len(ls_ans)):
    print(ls_ans[i])

Python: import sys
sys.setrecursionlimit(1000000)


def dfs(map, reached, pos):
    global dx, dy
    x, y = pos

    if reached[x][y] == 1 or map[x][y] == '0':
        return 0

    reached[x][y] = 1
    for dxi, dyi in zip(dx, dy):
        if not reached[x + dxi][y + dyi]:
            dfs(map, reached, (x + dxi, y + dyi))

    return int(map[x][y])


def island(map):
    w = len(map)
    h = len(map[0])
    reached = [[0] * h for _ in range(w)]

    ans = 0
    for x in range(w):
        for y in range(h):
            if reached[x][y] == 1 or map[x][y] == '0':
                continue
            ans += dfs(map, reached, (x, y))

    return ans


dx = [1, 0, -1, 0, 1, -1, 1, -1]
dy = [0, 1, 0, -1, 1, -1, -1, 1]

while True:
    w, h = map(int, input().split())

    if (0, 0) == (w, h):
        break

    mapp = ['0' * (w + 2)]
    for _ in range(h):
        mapp.append('0%s0' % input().replace(' ', ''))
    mapp.append('0' * (w + 2))
    print(island(mapp))

Python: import sys

sys.setrecursionlimit(10000)

def dfs(pos):
    x = pos[0]
    y = pos[1]
    C[y][x] = -1
    
    for x_d, y_d in [[1,0],[1,1],[0,1],[1,-1],[-1,1],[-1,0],[-1,-1],[0,-1]]:        
        x_new = x+x_d
        y_new = y+y_d

        if not 0<=x_new<=w-1:
            continue

        if not 0<=y_new<=h-1:
            continue

        if C[y_new][x_new]!=1:
            continue

        dfs([x_new, y_new])

while True:
    w, h = map(int, input().split())
    if w==h==0:
        break
    
    C = [list(map(int, input().split())) for _ in range(h)]
    
    counter = 0
    for w_i in range(w):
        for h_i in range(h):
            
            if C[h_i][w_i]==1:
                counter += 1
                dfs([w_i,h_i])
    
    print(counter)

Python: import sys
def f(n):
  if n%100==0:
    print>>sys.stderr, n
  f(n+1)
f(1)