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 math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    dd = [(-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1)]

    while True:
        w,h = LI()
        if w == 0:
            break
        a = [LI() for _ in range(h)]
        f = collections.defaultdict(bool)
        r = 0
        for i in range(h):
            for j in range(w):
                if f[(i,j)] or a[i][j] == 0:
                    continue
                r += 1
                q = [(i,j)]
                f[(i,j)] = True
                qi = 0
                while len(q) > qi:
                    ci,cj = q[qi]
                    qi += 1
                    for di,dj in dd:
                        ni = ci + di
                        nj = cj + dj
                        if 0 <= ni < h and 0 <= nj < w and a[ni][nj] == 1 and not f[(ni,nj)]:
                            f[(ni,nj)] = True
                            q.append((ni,nj))
        rr.append(r)

    return '\n'.join(map(str, rr))


print(main())

Python: import sys
sys.setrecursionlimit(50*50)

seen = []
field = []
dx =[0, 1, 1, 1, 0, -1, -1, -1]
dy =[1, 1, 0, -1, -1, -1, 0, 1]
global W, H

def dfs_grid(w, h):
    seen[h][w]=True
    #print(h, w)

    for i in range(8):
        nw = w + dx[i]
        nh = h + dy[i]

        if nw < 0 or nw >= W or nh < 0 or nh >= H:
            continue
        if field[nh][nw] == 0:
            continue
        if seen[nh][nw]:
            continue

        dfs_grid(nw, nh)
while True:
    W, H = map(int, input().split())
    cnt =0

    if W ==0 and H ==0:
        break

    for i in range(H):
        field.append(list(map(int,(input().split()))))
        seen.append([False]* W)

    #print(field)
    #print(seen)

    for i in range(H):
        for j in range(W):
            if field[i][j]==0:
                continue
            if not(seen[i][j]):
                dfs_grid(j, i)
                cnt +=1

    #print(seen)
    field.clear()
    seen.clear()
    print(cnt)

Python: while 1:
    W, H = list(map(int,input().split()))
    if W == 0:break
 
    C = [list(map(int, input().split())) for i in range(H) ]
     
    def work(x,y):
        ax = [-1, 0, 1, -1, 1, -1, 0, 1]
        ay = [-1, -1, -1, 0, 0, 1, 1, 1]
 
        if C[y][x] != 1:return False
        stack = [(x,y)]
 
        while len(stack) > 0:
            s = stack.pop(-1)
            C[s[1]][s[0]] = -1 #mark
 
            for _x in [s[0] + _x for _x in ax]:
                for _y in [s[1] + _y for _y in ay]:
                    if 0 <= _x < W and 0 <= _y < H and C[_y][_x] == 1:
                        stack.append((_x, _y))
        return True
#    def work(x,y):
#        ax = [-1, 0, 1, -1, 1, -1, 0, 1]
#        ay = [-1, -1, -1, 0, 0, 1, 1, 1]
#
#        if C[y][x] != 1:return False
#        C[y][x] = -1 #mark
#
#        for _ax in ax:
#            for _ay in ay:
#                if 0 <= _ax + x < W and 0 <= _ay + y < H:
#                    work(_ax + x, _ay + y)
#        return True
 
    cnt = 0
    for x in range(W):
        for y in range(H):
            cnt += work(x,y)
    print(cnt)

Python: def solve():
    from collections import deque
    
    while True:
        w, h = map(lambda x: int(x)+2, input().split())
        if w == 2:
            break

        result = 0
        a = ['0']*w
        for _ in [0]*(h-2):
            a += ['0'] + input().split() + ['0']
        a += ['0']*w

        for i in range(w*h):
            if a[i] != '1':
                continue
            a[i] = '0'
            result += 1
            dq = deque([i])
            while dq:
                j = dq.popleft()
                for dest in (j-w-1, j-w, j-w+1, j-1, j+1, j+w-1, j+w, j+w+1):
                    if a[dest] == '1':
                        a[dest] = '0'
                        dq.append(dest)
        print(result)


if __name__ == "__main__":
    solve()

Python: import sys
sys.setrecursionlimit(10**9)
ans = []
dx = (1,1,1,0,0,-1,-1,-1)
dy = (1,0,-1,1,-1,1,0,-1)

def dfs(x,y):
    st[x][y] = True
    for i,j in zip(dx,dy):
        nx = x + i
        ny = y + j
        if nx < 0 or h <= nx: continue
        if ny < 0 or w <= ny: continue
        if st[nx][ny]: continue
        if c[nx][ny]: dfs(nx,ny)

while True:
    w,h = map(int,input().split())
    if w == 0 and h == 0: break
    c = [[int(i) for i in input().split()] for _ in range(h)]
    st = [[False]*w for _ in range(h)]
    cnt = 0
    for i in range(h):
        for j in range(w):
            if st[i][j]: continue
            if c[i][j]:
                dfs(i,j)
                cnt += 1
    ans.append(cnt)

print("\n".join(map(str,ans)))

Python: # -*- coding: utf-8 -*-
def dfs(x,y):
    c[x][y] = 0
    for dx in [-1,0,1]:
        for dy in [-1,0,1]:
            nx = x + dx
            ny = y + dy
            if 0 <= nx < h and 0 <= ny < w and c[nx][ny] == 1:
                dfs(nx, ny)

while True:
    w, h = map(int,input().split())
    if w == 0 and h == 0 :
        break
    
    c = [list(map(int,input().split())) for j in range(h)]
    count = 0
    
    for i in range(h):
        for j in range(w):
            if c[i][j] == 1:
                dfs(i,j)
                count += 1

    print(count)

Python: #!python
# -*- coding: utf-8 -*-

try:
    import sys

    sys.setswitchinterval(10**8)


    next_s = ((-1, 1), (0, 1), (1, 1),
              (-1, 0), (1, 0),
              (-1, -1), (0, -1), (1, -1))


    def DFS(x, y):
        if not (0 <= x < W and 0 <= y < H):
            return
        if not map_[y * W + x]:
            return

        map_[y * W + x] = 0

        for dx, dy in next_s:
            DFS(x + dx, y + dy)


    while True:
        W, H = map(int, input().split())
        if W == 0 and H == 0:
            break

        map_ = []
        for h in range(H):
            map_ += list(map(int, input().split()))
        island_num = 0
        while any(map_):
            next_idx = map_.index(1)
            x = next_idx % W
            y = next_idx // W
            DFS(x, y)
            island_num += 1

        print(island_num)
except Exception as e:
    print(e)