import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
        String[][] board = {{"blue", "red", "orange", "red"}, {"red", "red", "blue", "orange"}, {"blue", "orange", "red", "red"}, {"orange", "orange", "red", "blue"}};
        int h = 1;
        int w = 1;
        
        String[][] board_1 = {{"yellow", "green", "blue"}, {"blue", "green", "yellow"}, {"yellow", "blue", "blue"}};
        int h_1 = 0;
        int w_1 = 1;
        
        System.out.println(solution(board, h, w));
        //System.out.println(solution(board_1, h_1, w_1));
    }
    
    public static int solution(String[][] board, int h, int w) {
        int answer = 0;
        
        int up = h - 1;
        int down = h + 1;
        int left = w - 1;
        int right = w + 1;
        
        String color = board[h][w];
        
        if(up >= 0 && board[up][w].equals(color)) {
            answer++;
        }
        if(down <= board.length-1 && board[down][w].equals(color)) {
            answer++;
        }
        if(left >= 0 && board[h][left].equals(color)) {
            answer++;
        }
        if(right <= board[0].length-1 && board[h][right].equals(color)) {
            answer++;
        }
        
        return answer;
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: