/*
Online Java - IDE, Code Editor, Compiler
Online Java is a quick and easy tool that helps you to build, compile, test your programs online. */
public class Main
{
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        String word = "FOAM";
        
        char [] wordChars = word.toCharArray();
        char [][] matrix = {{'F','O','A','M'},
                            {'O','B','Q','P'},
                            {'A','N','O','B'},
                            {'M','A','S','S'} };
    
        int count=0;
    
        if (word.length()>matrix.length || word.length()>matrix[0].length)
        {
            System.out.println("The word: " + word + " is too big for the matrix: " + matrix.length + " X " +
            matrix[0].length);
            System.exit(0);
        }
        
        System.out.println("******Checking each column*********");
        //check each row for match in word
        for (int i=0; i<matrix[0].length; i++)
        {
            System.out.println("\n***Checking column***: " + i);
            count=0;
            System.out.println("\nvalue i:" + i);
            System.out.println(count);

            for (int j=0; j<matrix.length; j++)
            {
                System.out.println("value j:" + j);
                System.out.println("Processing character from word: " + word.charAt(j));
                System.out.println("Processing character from matrix: " + matrix[i][j]);

                if (matrix[i][j]==word.charAt(j))
                {
                    count++;
                    System.out.println("count is: " + count);
                    System.out.println("length of word is: " + word.length());
                    
                    if (count==word.length())
                    {
                        System.out.println("\nMatch found in word: " + word + " on row: " + i);
                    }
                }
            }
        }
        
        System.out.println("\n***********Checking each row************");
        
        for (int i=0; i<matrix.length; i++)
        {
            System.out.println("\n***Checking row***: " + i);
            count=0;
            System.out.println("\nvalue i:" + i);
            
            for (int j=0; j<matrix[0].length; j++)
            {
                System.out.println("value j:" + j);
                System.out.println("Processing character from word: " + word.charAt(j));
                System.out.println("Processing character from matrix: " + matrix[j][i]);

                if (matrix[j][i]==word.charAt(count))
                {
                    count++;
                    System.out.println("count is: " + count);
                    System.out.println("length of word is: " + word.length());
                    
                    if (count==word.length())
                    {
                        System.out.println("\nMatch found in word: " + word + " on column: " + i +"\n");
                    }
                }
            }
        }
    }
}