NumberOfIslands { public int numIslands(char[][] grid) { if (grid.length == 0 || grid[0].length == 0) { return 0; } boolean[][] visited = new boolean[grid.length][grid[0].length]; int n = 0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if (grid[i][j] == '1' && !visited[i][j]) { dfs(grid, visited, i, j); n++; } } } return n; }  int numIslands(char[][] grid); void dfs(char[][] grid, boolean[][] visited, int r, int c);  }
@Test(dataProvider = "islandsAndResult") public void testNumber(char[][] islands, int expectedNumber) { assertEquals(new NumberOfIslands().numIslands(islands), expectedNumber); }