/* Class: COP3530
 * Date: Jan 13, 2006
 * Instructor: Arup Guha
 * TA: Adam Campbell
 *
 * Recitation #1, Problem #1
 **/

import java.io.*;
import java.util.StringTokenizer;

public class Rec1Prob1{

	/* I have main throw exception so I don't need the try/catch blocks everywhere when reading in from the file.
	 * In general, this is not good programming practice, but for these simple problems we are more interested in the
	 *   algorithm.
	 **/
	public static void main(String[] args) throws Exception{

		int[][] matrix;
		int n; // the number of rows, columns that the matrix has
		BufferedReader userInputReader, fileReader;
		String fileName;
		StringTokenizer tokenizer;

		// initialize the BufferedReader to read from standard input
		userInputReader = new BufferedReader(new InputStreamReader(System.in));

		// prompt the user for the file name, and get the file name from the user
		System.out.print("Please enter the file name: ");
		fileName = userInputReader.readLine();

		// open up the input file
		fileReader = new BufferedReader(new FileReader(fileName));

		// read n and initialize the matrix
		n = Integer.parseInt(fileReader.readLine());
		matrix = new int[n][n];

		for(int row = 0; row < n; row++){

			// read in the current line (row)
			tokenizer = new StringTokenizer(fileReader.readLine());

			for(int column = 0; column < n; column++){

				// parse the current number
				matrix[row][column] = Integer.parseInt(tokenizer.nextToken());

			}

		}

		// print out the output, and we're done!
		System.out.println("Row with most 1's: " + Rec1Prob1.MostOnes(matrix));

	}

	// Returns the index of the row with the most 1's
	// this works in O(n) time because the columnIndex never gets reset
	public static int MostOnes(int[][] binarytable){

		int rowWithMostOnes, columnIndex;

		rowWithMostOnes = 0;
		columnIndex = 0;

		// scan through every row, and set the columnIndex to location of the last 1 on this row
		for(int rowIndex = 0; rowIndex < binarytable.length; rowIndex++){

			// increment the column index until the end of the row is found or until a 0 is found
			while(columnIndex < binarytable[rowIndex].length && binarytable[rowIndex][columnIndex] == 1){

				rowWithMostOnes = rowIndex;
				columnIndex++;

			}

		}

		return rowWithMostOnes;

	}

}
