privatebooleandfs(int i, int j, int index){ if (board[i][j] != word[index]) returnfalse; if (index == word.length - 1) returntrue; this.visited[i][j] = true; boolean result = false; for (int[] direction : directions) { int x = i + direction[0]; int y = j + direction[1]; if (x < 0 || x > m - 1 || y < 0 || y > n - 1 || visited[x][y]) continue; if(dfs(x, y, index + 1)) returntrue; } this.visited[i][j] = false; returnfalse; } }