Language: Java

import java.io.*;                                                                                                             
import java.util.*;                                                                                                           
import java.math.*;                                                                                                           
public class Main {                                                                                                           
    public static void main(String[] args) throws java.io.IOException{                                                        
        Scanner scan = new Scanner(System.in);                                                                                
                                                                                                                              
        while(true){                                                                                                          
            int M = scan.nextInt();                                                                                           
            int M = scan.nextInt();
            int N = scan.nextInt();
            if(N==0 && M ==0)break;
            int ans =0;
            char [][] field =new char[M][N];
            for(int i=0;i<M;i++){
                for(int j=0;j<N;j++){
                    field[i][j] = scan.nextByte();
                }
                scan.skip("\n");
            }

            for(int i=0;i<M;i++){
                for(int j=0;j<N;j++){
                    if(field[i][j] != '.'){
                        dfs(i,j,field);
                        ans++;
                    }
                }
            }
            System.out.println(ans);
    }


}
  public static void dfs(int x,int y,char [][] field){
      char crop = field[y][x];
      field[y][x] = '.';
      int  [] dx= {1,0,-1,0};
      
      for(int i=0;i<3;i++){
          int nx = x+dx;
          int ny = x + dx^1;
          if(0<=nx 
            && nx < N 
            &&0<=ny 
            &&ny <M 
            && field[ny][nx].equals(crop)){
              dfs(ny,nx,field);
          }
      }
  }
}

Compiler output:
Main.java:10: error: variable M is already defined in method main(String[])
            int M = scan.nextInt();
                ^
Main.java:17: error: incompatible types: possible lossy conversion from byte to char
                    field[i][j] = scan.nextByte();
                                               ^
Main.java:41: error: bad operand types for binary operator '+'
          int nx = x+dx;
                    ^
  first type:  int
  second type: int[]
Main.java:42: error: bad operand types for binary operator '+'
          int ny = x + dx^1;
                     ^
  first type:  int
  second type: int[]
Main.java:44: error: cannot find symbol
            && nx < N 
                    ^
  symbol:   variable N
  location: class Main
Main.java:46: error: cannot find symbol
            &&ny <M 
                  ^
  symbol:   variable M
  location: class Main
Main.java:47: error: char cannot be dereferenced
            && field[ny][nx].equals(crop)){
                            ^
7 errors