You are given an m x n binary matrix grid.

A move consists of choosing any row or column and toggling each value in that row or column (i.e., changing all 0's to 1's, and all 1's to 0's).

Every row of the matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers.

Return the highest possible score after making any number of moves (including zero moves).

&nbsp;
Example 1:

Input: grid = [[0,0,1,1],[1,0,1,0],[1,1,0,0]]
Output: 39
Explanation: 0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39


Example 2:

Input: grid = [[0]]
Output: 1


&nbsp;
Constraints:


	m == grid.length
	n == grid[i].length
	1 &lt;= m, n &lt;= 20
	grid[i][j] is either 0 or 1.


class Solution:

    def calcScore(self):
        score = 0
        
        for row in self.grid:
            for i in range(self.width):
                score += row[i] * (2 ** (self.width - i - 1))
        return score
    
    def flipRow(self, row):
        for i in range(self.width):
            self.grid[row][i] = int(not self.grid[row][i])
        
    def flipCol(self, col):
        for i in range(self.height):
            self.grid[i][col] = int(not self.grid[i][col])
            
    def colSum(self, col):
        total = 0
        
        for i in range(self.height):
            total += self.grid[i][col]
            
        return total
    
    def matrixScore(self, grid: List[List[int]]) -> int:
        self.grid = grid
        self.height = len(grid)
        self.width = len(grid[0])
        
        for r in range(self.height):
            if not self.grid[r][0]:
                if not self.grid[r][0]:
                    self.flipRow(r)
                    
        for c in range(1, self.width):
            colSum = self.colSum(c)
            
            if colSum < ceil(self.height/2):
                self.flipCol(c)
            
        return self.calcScore()
        
class Solution {
    public int matrixScore(int[][] grid) {
        ArrayList<Integer> arr=new ArrayList<Integer>();
        for(int i=0;i<grid.length;i++)
        {
            boolean flip=false;
            if(grid[i][0]==0)
            {
                grid[i][0]^=1;
                flip=true;
            }
            if(flip)
            {
             for(int j=1;j<grid[0].length;j++)
            {
                grid[i][j]^=1;
            }
            }
        }
        for(int i=1;i<grid[0].length;i++)
        {
            int zero=0;
            int one=0;
            for(int j=0;j<grid.length;j++)
            {
                if(grid[j][i]==0)
                {
                    zero++;
                }
                else
                {
                    one++;
                }
            }
            if(zero>one)
            {
                arr.add(i);
            }
        }
        for(int i:arr)
        {
            for(int j=0;j<grid.length;j++)
            {
                grid[j][i]^=1;
            }
        }
        int sum=0;
        for(int i=0;i<grid.length;i++)
        {
            String num="";
            for(int j=0;j<grid[0].length;j++)
            {
                num+=String.valueOf(grid[i][j]);
            }
            sum+=Integer.valueOf(num,2);
        }
        return sum;
    }
}
class Solution {
public:
    
    int sum(vector<vector<int>> mat){
        int score = 0;
        
        
        
        for(int i=0;i<mat.size();i++){
            int p = mat[0].size()-1;
            for(int j=0;j<mat[0].size();j++){
                score+= mat[i][j]*pow(2,p);
                p--;
            }
        }
        
        return score;
    }
    
    int matrixScore(vector<vector<int>>& grid) {
//         if(grid.size()==1){
//             return ;
//         }
        
        
        for(int i=0;i<grid.size();i++){
            if(grid[i][0]==0){
               for(int j=0;j<grid[0].size();j++){
                   if(grid[i][j]==0){
                       grid[i][j]=1;
                   }
                   else{
                        grid[i][j]=0;
                   }
               }
            }
        }
        
        
        
        
        vector<int> ind;
        
        for(int i=1;i<grid[0].size();i++){
            int ct=0;
            for(int j=0;j<grid.size();j++){
                ct+=grid[j][i];
               
            }
            if(ct<=grid.size()/2){
                ind.push_back(i);
            }
        }
        for(auto v:ind){
            cout<<v<<" ";
        }
        
        for(auto c:ind){
            for(int j=0;j<grid.size();j++){
                if(grid[j][c]==0){
                       grid[j][c]=1;
                   }
                   else{
                        grid[j][c]=0;
                   }
            }
        }
       
        
        
        
        
        return sum(grid);
        
        
        
        
        
        
       
    }
var matrixScore = function(grid) {
let flip = (i,rowOrCol) => {// helper function for flipping row and column
    if(rowOrCol){
        for(let idx in grid[i]){
            grid[i][idx] = grid[i][idx]===1 ? 0 : 1;
        }
    }
    else{
        for(let idx in grid){
            grid[idx][i] = grid[idx][i]===1 ? 0 : 1;
        }
    }
}

for(let i=0; i<grid.length; i++){// flip rows only if leftmost bit is 0
    if(grid[i][0]!==1){
        flip(i,true);
    }
}

for(let i=1; i<grid[0].length; i++){//flip column only if the number of 1s is less than 0s, as all bits hold equal values in a column
    let ones = 0;
    for(let j in grid){
        ones += grid[j][i];
    }
    if(ones<grid.length/2) flip(i,false);
}

return grid.reduce((a,b) => a + parseInt(b.join(''),2),0);
};
Score After Flipping Matrix