Go: // AOJ 1160: How Many Islands? http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160

package main

import (
	"bufio"
	"container/list"
	"fmt"
	"os"
	"strconv"
)

var SEA = 0
var LAND = 1

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func discoverIslands(M, V [][]int, H, W int) int {
	// 島を探す
	queue := list.New()
	islands := 0
	for i := 1; i <= H; i++ {
		for j := 1; j <= W; j++ {
			// 訪問済みの土地ならスキップ
			if V[i][j] == 1 {
				continue
			}

			// 訪問中の土地が海なら訪問済みにしてスキップ
			if M[i][j] == SEA {
				V[i][j] = 1
				continue
			}

			// 未訪問の陸地が見つかったので
			// 島の数を1つ増やし、この陸地から辿れる陸地を訪問していく
			islands++
			queue.PushBack([]int{i, j})

			for queue.Len() > 0 {
				cur := queue.Remove(queue.Front()).([]int)
				h, w := cur[0], cur[1]
				if V[h][w] == 1 {
					continue
				}
				V[h][w] = 1

				// 全方向に未訪問の陸地がないか探索
				for v := -1; v <= 1; v++ {
					for l := -1; l <= 1; l++ {
						dh := h + v
						dw := w + l
						if M[dh][dw] == LAND && V[dh][dw] == 0 {
							queue.PushBack([]int{dh, dw})
						}
					}
				}
				/*
					if M[h-1][w] == LAND {
						queue.PushBack([]int{h - 1, w})
					}
					if M[h-1][w+1] == LAND {
						queue.PushBack([]int{h - 1, w + 1})
					}
					if M[h][w+1] == LAND {
						queue.PushBack([]int{h, w + 1})
					}
					if M[h+1][w+1] == LAND {
						queue.PushBack([]int{h + 1, w + 1})
					}
					if M[h+1][w] == LAND {
						queue.PushBack([]int{h + 1, w})
					}
					if M[h+1][w-1] == LAND {
						queue.PushBack([]int{h + 1, w - 1})
					}
					if M[h][w-1] == LAND {
						queue.PushBack([]int{h, w - 1})
					}
					if M[h-1][w-1] == LAND {
						queue.PushBack([]int{h - 1, w - 1})
					}
				*/
			}
		}
	}
	return islands
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	for {
		W, H := nextInt(sc), nextInt(sc)

		if W == 0 && H == 0 {
			break
		}
		// Initialize map and visted list
		M := make([][]int, H+2)
		V := make([][]int, H+2)
		for i := 0; i < H+2; i++ {
			M[i] = make([]int, W+2)
			V[i] = make([]int, W+2)
		}
		for i := 1; i <= H; i++ {
			for j := 1; j <= W; j++ {
				M[i][j] = nextInt(sc)
			}
		}

		islands := discoverIslands(M, V, H, W)

		fmt.Println(islands)
	}
}

Python: import sys
def dfs(tiles, W, H, x, y):
    tiles[y][x] = '0'
    for dx,dy in ((-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)):
        if 0<=x+dx<W and 0<=y+dy<H and tiles[y+dy][x+dx]=='1':
            dfs(tiles, W, H, x+dx, y+dy)
    return

def main():
    while True:
        W,H = [int(x) for x in input().split()]
        if W==0 and H==0:
            exit()
        tiles = [input().split() for h in range(H)]
        cnt = 0
        for h in range(H):
            for w in range(W):
                if tiles[h][w] == '1':
                    cnt += 1
                    dfs(tiles, W, H, w, h)
        print(cnt)

if __name__ == '__main__':
    sys.setrecursionlimit(10000)
    main()

Python: # coding: utf-8
import sys
sys.setrecursionlimit(2147483647)
def check(p):
    global field
    if(field[p[1]][p[0]]=='1'):
        field[p[1]][p[0]]='0'
    if(0<=p[0]+1<w and field[p[1]][p[0]+1]=='1'):
        check((p[0]+1,p[1]))
    if(0<=p[0]-1<w and field[p[1]][p[0]-1]=='1'):
        check((p[0]-1,p[1]))
    if(0<=p[1]+1<h and field[p[1]+1][p[0]]=='1'):
        check((p[0],p[1]+1))
    if(0<=p[1]-1<h and field[p[1]-1][p[0]]=='1'):
        check((p[0],p[1]-1))
    if(0<=p[0]+1<w and 0<=p[1]-1<h and field[p[1]-1][p[0]+1]=='1'):
        check((p[0]+1,p[1]-1))
    if(0<=p[0]-1<w and 0<=p[1]-1<h and field[p[1]-1][p[0]-1]=='1'):
        check((p[0]-1,p[1]-1))
    if(0<=p[0]+1<w and 0<=p[1]+1<h and field[p[1]+1][p[0]+1]=='1'):
        check((p[0]+1,p[1]+1))
    if(0<=p[0]-1<w and 0<=p[1]+1<h and field[p[1]+1][p[0]-1]=='1'):
        check((p[0]-1,p[1]+1))
     
while 1:
    ans=0
    w,h=map(int,input().split())
    if w==0:
        break
    field=[]
    for i in range(h):
        s=''.join(input().split())
        field.append(list(s))
    for i in range(h):
        for j in range(w):
            if field[i][j]=='1':
                check((j,i))
                ans+=1
    print(ans)

Python: import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)


def dfs(h, w):
    board[h][w] = 0
    for dh in range(-1, 2):
        for dw in range(-1, 2):
            nh = h + dh
            nw = w + dw
            if 0 <= nh < H and 0 <= nw < W and board[nh][nw] == 1:
                dfs(nh, nw)


while True:
    W, H = map(int, input().split())
    if H == 0 and W == 0:
        break
    board = [list(map(int, input().split())) for _ in range(H)]
    ans = 0
    for h in range(H):
        for w in range(W):
            if board[h][w]:
                dfs(h, w)
                ans += 1
    print(ans)

Python: d = [-1,0,1]
def search(x,y,m):
    if not(0<=x<len(m[0]) and 0<=y<len(m)) or m[y][x]=='0': return
    m[y][x] = '0'
    for dy in d:
        for dx in d:
            search(x+dx,y+dy,m)
while True:
    w,h = map(int,input().split())
    if w==h==0: break
    m = [input().split() for _ in range(h)]
    c = 0
    for y in range(h):
        for x in range(w):
            if m[y][x]=='1':
                c += 1
                search(x,y,m)
    print(c)

Python: import sys
from itertools import product as pro
sys.setrecursionlimit(10**7)

while True:
    w,h=map(int,input().split())
    if w==0 and h==0:
        sys.exit()
    
    G=[]
    for i in range(h):
        G.append(list(map(int,input().split())))

    #隣接する島
    def sur(X):
        x=X[0]
        y=X[1]
        axis_x=[x+1,x,x-1]
        axis_y=[y+1,y,y-1]
        preE=list(pro(axis_x,axis_y))
        preE.remove((x,y))
        E=[]
        for e in preE:
            if 0<=e[0]<=h-1 and 0<=e[1]<=w-1:
                if G[e[0]][e[1]]==1:
                    E.append(e)
        return E

    check=[[False]*w for i in range(h)]

    #checkの陸埋めをしても良い
    def dfs(v):
        check[v[0]][v[1]]=True
        for next in sur(v):
            if check[next[0]][next[1]]:
                continue
            dfs(next)

    ans=0
    for i in range(h):
        for j in range(w):
            if not check[i][j] and G[i][j]==1:
                ans+=1
                dfs((i,j))
    
    print(ans)

Python: import sys
sys.setrecursionlimit(10**6)

w=0;h=0;
def dfs(x,y,Map):
    #ending condition
    if not (0 <= x and x < h and 0 <= y and y < w):
        return
    if Map[x][y] == 0:
        return
    
    #operation
    Map[x][y] = 0

    #moving deep
    for i in [-1,0,1]:
        for j in [-1,0,1]:
            dfs(x+i,y+j,Map)

def start_cnt():
    global w,h
    w,h = map(int,input().split())
    if w == 0 and h == 0:exit();
    cnt = 0
    Map = []
    for i in range(h):
            Map.append(list(map(int,input().split())))
    for i in range(h):
        for j in range(w):
            if Map[i][j] == 1:
                dfs(i,j,Map)
                cnt += 1
    print(cnt)
while True:
    start_cnt()

Python: import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**6)
def dfs(a,v):
    global st,per
    if v in st: st.remove(v)
    else: return
    for tom in per:
        if a[v[0]+tom[0]][v[1]+tom[1]]==1: dfs(a,(v[0]+tom[0],v[1]+tom[1]))
while True:
    c,r=map(int,input().split())
    if c==0 and r==0 : exit(0)
    a=[[0]*(c+2)]
    for j in range(r):
        a.append([0]+[int(i) for i in input().split()]+[0])
    a.append([0]*(c+2))
    st=set()
    for i in range(len(a)):
        for j in range(len(a[i])):
            if a[i][j]==1:
                st.add((i,j))
    per=[[1,1],[1,0],[0,1],[-1,1],[1,-1],[-1,-1],[-1,0],[0,-1]]
    ans=0
    while st:
        ans+=1
        for i in st:
            dfs(a,i)
            break
    print(ans)