what is wrong with this java code?
<p>I was going through an ICPC question <a href="http://www.spoj.com/problems/AMR12G/" rel="nofollow">HERE</a></p>

<p>The problem boils down to finding maximum.'s which can be achieved with K operations in a matrix having * and . such that we are allowed to toggle elements in a row in one operation.I first toggled the rows which result in positive or zero(toggling a row 2 times gives 0 change) change in *'s in decreasing order. At the end, if there is 1 toggle left, toggle the row with the least absolute value amongst the rows having negative changes.</p>

<p>Here is my code - <a href="http://ideone.com/etDkSF" rel="nofollow">BELOW CODE IN IDEONE.COM WITH SOME TEST CASES</a></p>

<pre><code>import java.util.Scanner;


public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    for (t = t; t &gt; 0; t--) { 
      int n = sc.nextInt();
      int m = sc.nextInt();
      int k = sc.nextInt();
      char[][] array = new char[n][m] ;
      int[] tracker = new int[n];
      int[] trackerl = new int[n];
      int i,j;
      for (i = 0; i &lt; n; i++) {
        String temp = sc.next();
        for (j = 0; j &lt; m; j++) {             
          array[i][j] = (temp.charAt(j));  
        }   
      }
      int light = 0, diamond = 0;

      for (i = 0; i &lt; n; i++) {
        for (j = 0; j &lt; m; j++) {
          if (array[i][j] == '*') {
            diamond++;
          }     
        }
        tracker[i] = diamond;
        diamond = 0;
      }


      int a, b;
      int temp;
      int sortTheNumbers = n;

      for (a = 1; a &lt; sortTheNumbers; a++) {
        for (b = 0; b &lt; sortTheNumbers-a; b++) {
          if (tracker[b] &gt; tracker[b + 1]) {
            temp = tracker[b];
            tracker[b] = tracker[b + 1];
            tracker[b + 1] = temp;
          }
        }
      }

      for (i = 0; i &lt; n; i++)
        trackerl[i]=m-tracker[i];

      int br = 0;
      try {
        if (m % 2 == 0) {
          for ( i = 0; i &lt;= n - 1; i++)
            if (tracker[i] &gt; (m) / 2)
              br++;  
        }
        if (m % 2 !=0 ) {
          for (i = 0; i &lt;= n - 1; i++)
            if (tracker[i] &gt;= (m + 1) / 2)
              br++;
        }
      } catch (Exception e) {
      }

      int ans = 0;
      try {
        if (br &gt;= k) {
          for (i = n - 1; i &gt; (n - 1 - k); i--)
            ans += tracker[i];

          for (i = (n - 1 - k); i &gt;= 0; i--)
            ans += trackerl[i];       
        }

        if (br &lt; k) {
          if (br != 0 &amp;&amp; br != n) {
            for (i = n - 1; i &gt; (n - br); i--) {
              ans += tracker[i];
              k--;
            }

            int pass1 = 0, pass2 = 0;
            if (k % 2 == 0)
              pass1 = Math.max(tracker[(n - br)] + tracker[(n - br - 1)],
                                        trackerl[(n - br)] + trackerl[(n - br - 1)]);
              if (k % 2 != 0)
                pass2 = Math.max(tracker[(n - br)] + trackerl[(n - br - 1)],
                                          trackerl[(n - br)] + tracker[(n - br - 1)]);
                // System.out.print("Hp" + tracker[(n - br)]);
              }

              ans += Math.max(pass1, pass2);
              for (i = (n - 2 - br); i &gt;= 0; i--)
                ans += trackerl[i];
              }
              if (br != 0 &amp;&amp; br == n) {
                for (i = n - 1; i &gt; (n - br); i--) {
                  ans += tracker[i];
                  k--;
                }
                if (k % 2 != 0) {
                  ans += tracker[(n - br)];
                }
                if (k % 2 == 0) {
                  ans += trackerl[(n - br)];
                }
                for (i = (n - 1 - br); i &gt;= 0; i--) {
                  ans += trackerl[i];
                }        
              }
              if (br == 0) {
                if (k % 2 != 0) {
                  ans += tracker[(n - 1)];
                }
                if (k % 2 == 0) {
                  ans += trackerl[(n - 1)];
                }
                for (i = (n - 2); i &gt;= 0; i--) {
                  ans += trackerl[i];
                }
              }
            }
          } catch (Exception e) {
          }
          System.out.println(""+ans);
        }
      }
    }
</code></pre>

<p>Whichever test case I choose gives the correct answer. I maintained variety in my test cases, as you can see from the link.Still the code isn't correct as it is not accepted by the judge. I really can't find where is the flaw.Is it in my code or my logic? Please point it out for me.</p>
