Count number of 1’s in matrix

Algorithm

Suppose each row of an nXn array A consists of 1's and 0's such that in any row I of A all the 1's come before any 0's in that row. Suppose further that the number of 1's in the row I is atleast the number in row I+1,for I=0,1....n-2.Assuming A is already in memory, describe a method running in O(n) time for counting the number of 1's in the array.
Input(arr,size)// arr is 2D array of size (size). 
1.	Repeat steps 2 -4 for i=0 to size;
2.	Repeat  step 3 for j=0 to size;
3.	[input array elements as 0 or 1] cin>>arr[i][j];
4.	[end of step 1]
5.	[end]

Algo(a,n)
1.	Initialize count:=0;
2.	Call input(a,n);
3.	Repeat steps 4 and 5 for i=n to 0
4.	Initialize j:=0;
5.	Repeat while(a[i][j]==1  && j<n)
a.	[Increment count and j]count++ ,j++.
6.	Return count;
7.	[end]

Algorithm Description


Java Code

//to count the number of ones in a two dimensional array
import java.io.*;
class count
{
	public static void main(String args[]) throws Exception
	{
		int n,i,total=0,j;
		System.out.println("enter the value of n for an nXn array");
		BufferedReader cin= new BufferedReader (new InputStreamReader(System.in));
		n= Integer.parseInt(cin.readLine());
		int a[][]=new int[n][n];
		System.out.println("enter the elements(0 or 1) in the array");
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++){
				a[i][j]= Integer.parseInt(cin.readLine());	
			}
		}
		for(i=n-1,j=0;i>=0 && j<n;){
			if(a[i][j]==1){
				j++;
				total+=i+1;
			}
			else
				i--;
		}	
		System.out.println("the number of 1's int array are "+total);
	}
}