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: from collections import deque

def main(H,W):
  area = [[] for i in range(H)]
  island = []
  
  for _ in range(H):
    h = list(map(int,input().split()))
    area[_] = h
    for i in range(W):
      if h[i] == 1:
        island.append([_,i])
  
  visited = [[0] * W for i in range(H)]
  
  def dfs(area, visited,sh,sw):
    stack = deque([[sh,sw]])
    visited[sh][sw] = 1
    while stack:
      h,w = stack.pop()
      for j in range(-1,2):
        for k in range(-1,2):
          new_h, new_w = h + j, w + k
          if new_h < 0 or new_h >= H or \
            new_w < 0 or new_w >= W:
              continue
          elif area[new_h][new_w] == 1 and \
            visited[new_h][new_w] == 0:
            visited[new_h][new_w] = 1
            stack.append([new_h, new_w])
    return 
  
  ans = 0
  
  for i in island:
    sh,sw = i[0],i[1]
    if visited[sh][sw] == 0:
      dfs(area,visited,sh,sw)
      ans += 1
  
  print(ans)
  
while True:
  W,H = map(int,input().split())
  if H == 0 and W == 0:
    break
  else:
    main(H,W)

Python: import sys
from collections import deque
sys.setrecursionlimit(10**9)

dx = [1,1,0,-1,-1,-1,0,1]
dy = [0,-1,-1,-1,0,1,1,1]
  
def DFS(y,x):
  if table[y][x] == 0:
    return
  table[y][x] = 0
  for i in range(8):
    nx = x + dx[i]
    ny = y + dy[i]
    if 0<=ny<h and 0<=nx<w:
      DFS(ny, nx)
while True:  
  w, h = map(int, input().split())
  if w == 0 and h == 0:
    exit()
  table = []
  ans = 0
  for i in range(h):
    c = list(map(int, input().split()))
    table.append(c)
  for y in range(h):
    for x in range(w):
      if table[y][x] == 1:
        ans += 1
        DFS(y,x)
  print(ans)

Python: import sys
sys.setrecursionlimit(5000)

input = sys.stdin.readline
sea_map = []
chk_map = []
h = 0
w = 0
def chk_island(i,j):
    chk_map[i][j] = False
    if sea_map[i][j] == 0:
        return 0
    for d in ((-1,0), (-1,1), (0,1), (1,1), (1,0), (1,-1), (0,-1), (-1,-1)):
        if 0 <= i + d[0] < h and 0 <= j + d[1] < w:
            if chk_map[i+d[0]][j+d[1]]:
                chk_island(i+d[0],j+d[1])
    return 1

while True:
    count = 0
    w, h = map(int,input().split())
    if w == 0 and h == 0:
        break
    sea_map = [list(map(int,input().split())) for _ in range(h)]
    chk_map = [[True for _ in range(w)] for _ in range(h)]

    for i in range(h):
        for j in range(w):
            if chk_map[i][j]:
                count += chk_island(i,j)
    print (count)

Python: import sys
sys.setrecursionlimit(10000)

def dfs(x,y):
	for i in range(8):
		nx=x+dx[i]
		ny=y+dy[i]
		if 0<=nx<h and 0<=ny<w and C[nx][ny]==1 and D[nx][ny]==0:
			D[nx][ny]=d
			dfs(nx,ny)

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

while True:
	w,h=map(int,input().split())
	if w==0 and h==0:
		exit()
	d=0
	C=[list(map(int,input().split())) for i in range(h)]
	D=[[0]*w for i in range(h)]
	for x in range(h):
		for y in range(w):
			if C[x][y]==1 and D[x][y]==0:
				d+=1
				dfs(x,y)
	print(d)

Python: import sys
sys.setrecursionlimit(500000)

def allblue(H, W, C):
    for h in range(H):
        for w in range(W):
            if C[h][w] == 1:
                return (h, w)
    return (-1, -1)

directions = [(0, 1), (0, -1), (1, 1), (1, 0), (1, -1), (-1, -1), (-1, 0), (-1, 1)]
def dfs(h, w, C):
    C[h][w] = 0
    for dh, dw in directions:
        if 0 <= h+dh < H and 0 <= w+dw < W and C[h+dh][w+dw] == 1:
            dfs(h+dh, w+dw, C)

while True:
    W, H = map(int, input().split())
    if W == 0 and H == 0:
        exit()
    C = [list(map(int, input().split())) for i in range(H)]

    ans = 0
    while True:
        h, w = allblue(H, W, C)
        if (h, w) == (-1, -1):
            break
        dfs(h, w, C)
        ans += 1

    print(ans)

Python: import sys
sys.setrecursionlimit(10000)
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 dx,dy in [[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]]:
        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: 
if __name__ == "__main__":
    while True:
        w, h = map(int, input().split())
        if w == 0 and h == 0:
            break
        c = []
        for _ in range(h):
            hi = list(map(int, input().split()))
            c.append(hi)
        
        visited = []
        for _ in range(h):
            hi = [False] * w
            visited.append(hi)
        
        st = []
        ans = 0

        for s_row in range(h):
            for s_col in range(w):
                if c[s_row][s_col] != 0 and not visited[s_row][s_col]:
                    st = [(s_row, s_col)]
                    while st:
                        now = st.pop()
                        for i in range(-1, 2):
                            for j in range(-1, 2):
                                if now[0] + i >= 0 and now[0] + i < h and now[1] + j >= 0 and now[1] + j < w and not visited[now[0] + i][now[1] + j] and c[now[0] + i][now[1] + j] != 0:
                                    visited[now[0] + i][now[1] + j] = True
                                    st.append((now[0] + i, now[1] + j))
                    ans += 1
        print(ans)