public int findMaxScore(String[] grid){
        int n = grid.length;
        if(n==0) return 0;
        int m = grid[0].length();
        int N = n*m;
        int E = 0;
        Edge[] edges = new Edge[(n-1)*m+(m-1)*n];
        for(int i=0; i<n; i++){
            m = grid[i].length();
            for(int j=0; j<m; j++){
                int u = i*m+j;
                if(i+1<n){
                    int v = (i+1)*m+j;
                    int w = Math.abs(getValue(grid[i].charAt(j)) - getValue(grid[i+1].charAt(j)));
                    edges[E++] = new Edge(u,v,w);

                }
                if(j+1<m){
                    int v = i*m+j+1;
                    int w = Math.abs(getValue(grid[i].charAt(j)) - getValue(grid[i].charAt(j+1)));
                    edges[E++] = new Edge(u,v,w);
                }
            }
        }
        assert E==(n-1)*m+(m-1)*n;
        parent = new int[N];
        for(int i=0; i<N; i++){
            parent[i] = i;
        }
        Arrays.sort(edges);
        int result=0;
        for(int i=0; i<E; i++){
            if(find(edges[i].u)!=find(edges[i].v)){
                result+=edges[i].w;
                union(edges[i].u, edges[i].v);
            }
        }

        return result;
    }